Merge pull request #489 from nurazon59/test/fix-rm-command-noop

fix(test): exercise `ghq rm` in command tests
This commit is contained in:
Masayuki Matsuki 2026-04-19 17:35:25 +09:00 committed by GitHub
commit bbe6528a13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -41,13 +41,25 @@ func TestRmCommand(t *testing.T) {
name: "simple",
input: []string{"rm", "motemen/ghqq"},
setup: func(t *testing.T) {
os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqq"), 0755)
if err := os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqq", ".git"), 0755); err != nil {
t.Fatal(err)
}
},
expectErr: false,
},
{
name: "empty directory",
input: []string{"rm", "motemen/ghqqq"},
name: "empty directory",
input: []string{"rm", "motemen/ghqqq"},
setup: func(t *testing.T) {
if err := os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqqq"), 0755); err != nil {
t.Fatal(err)
}
},
expectErr: true,
},
{
name: "missing directory",
input: []string{"rm", "motemen/missing"},
setup: func(t *testing.T) {},
expectErr: true,
},
@ -87,6 +99,21 @@ func TestRmCommand(t *testing.T) {
if tc.cmdRun != nil {
cmdutil.CommandRunner = tc.cmdRun
}
var runErr error
_, _, err := captureWithInput([]string{"y"}, func() {
a := newApp()
args := append([]string{"ghq"}, tc.input...)
runErr = a.Run(context.Background(), args)
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotErr := runErr != nil; gotErr != tc.expectErr {
t.Fatalf("error = %v, expectErr = %v", runErr, tc.expectErr)
}
})
}
}
@ -119,7 +146,9 @@ func TestRmDryRunCommand(t *testing.T) {
name: "simple",
input: []string{"rm", "--dry-run", "motemen/ghqq"},
setup: func(t *testing.T) {
os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqq"), 0755)
if err := os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqq", ".git"), 0755); err != nil {
t.Fatal(err)
}
},
expectErr: false,
},
@ -139,9 +168,9 @@ func TestRmDryRunCommand(t *testing.T) {
},
{
name: "permission denied",
input: []string{"rm", "--dry-run", "motemen/ghqq"},
input: []string{"rm", "--dry-run", "motemen/ghq-notpermitted"},
setup: func(t *testing.T) {
f := filepath.Join(tmpd, "github.com", "motemen", "ghqq")
f := filepath.Join(tmpd, "github.com", "motemen", "ghq-notpermitted")
os.MkdirAll(f, 0000)
t.Cleanup(func() {
os.Chmod(f, 0755)
@ -165,6 +194,21 @@ func TestRmDryRunCommand(t *testing.T) {
if tc.cmdRun != nil {
cmdutil.CommandRunner = tc.cmdRun
}
var runErr error
_, _, err := capture(func() {
a := newApp()
args := append([]string{"ghq"}, tc.input...)
runErr = a.Run(context.Background(), args)
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotErr := runErr != nil; gotErr != tc.expectErr {
t.Fatalf("error = %v, expectErr = %v", runErr, tc.expectErr)
}
})
}
}