-
Notifications
You must be signed in to change notification settings - Fork 0
fix: Windows scanner compatibility #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1f8060e
d880171
a01be68
0902627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| //go:build windows | ||
|
|
||
| package scanner | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "reflect" | ||
| "sort" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestScanDirs_WindowsExecExtensions(t *testing.T) { | ||
| dir := t.TempDir() | ||
|
|
||
| // Create files with various Windows executable extensions | ||
| exts := []string{".exe", ".cmd", ".bat"} | ||
| for _, ext := range exts { | ||
| name := "tool" + ext | ||
| if err := os.WriteFile(filepath.Join(dir, name), []byte(""), 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| // Create a non-executable file | ||
| if err := os.WriteFile(filepath.Join(dir, "readme.txt"), []byte(""), 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| names := ScanDirs([]string{dir}) | ||
| nameSet := make(map[string]bool) | ||
| for _, n := range names { | ||
| nameSet[n] = true | ||
| } | ||
|
|
||
| for _, ext := range exts { | ||
| want := "tool" + ext | ||
| if !nameSet[want] { | ||
| t.Errorf("expected %q in results", want) | ||
| } | ||
| } | ||
| if nameSet["readme.txt"] { | ||
| t.Error(".txt file should not be included") | ||
| } | ||
| } | ||
|
|
||
| func TestFilter_WindowsExtensionStripping(t *testing.T) { | ||
| dir := t.TempDir() | ||
| t.Setenv("PATH", dir) | ||
|
|
||
| for _, name := range []string{"curl.exe", "git.exe", "jq.exe"} { | ||
| if err := os.WriteFile(filepath.Join(dir, name), []byte(""), 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| input []string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "names without extension", | ||
| input: []string{"curl", "git", "jq", "nonexistent"}, | ||
| want: []string{"curl", "git", "jq"}, | ||
| }, | ||
| { | ||
| name: "names with extension", | ||
| input: []string{"curl.exe", "git.exe", "jq.exe", "nonexistent.exe"}, | ||
| want: []string{"curl.exe", "git.exe", "jq.exe"}, | ||
| }, | ||
| { | ||
| name: "mixed names", | ||
| input: []string{"curl", "git.exe", "nonexistent"}, | ||
| want: []string{"curl", "git.exe"}, | ||
| }, | ||
| { | ||
| name: "no matches", | ||
| input: []string{"foo", "bar"}, | ||
| want: []string{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got := Filter(tc.input) | ||
|
|
||
| if len(got) == 0 && len(tc.want) == 0 { | ||
| return | ||
| } | ||
|
|
||
| sort.Strings(got) | ||
| sort.Strings(tc.want) | ||
| if !reflect.DeepEqual(got, tc.want) { | ||
| t.Errorf("Filter() = %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
Comment on lines
+47
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このテスト テストの堅牢性を高めるため、 以下に、テーブル駆動テストを用いたリファクタリング案を示します。この変更により、テストの可読性が向上し、さまざまなケースを網羅しやすくなります。 注: この変更には func TestFilter_WindowsExtensionStripping(t *testing.T) {
dir := t.TempDir()
t.Setenv("PATH", dir)
executables := []string{"curl.exe", "git.exe", "jq.exe"}
for _, name := range executables {
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(""), 0644); err != nil {
t.Fatal(err)
}
}
testCases := []struct {
name string
input []string
want []string
}{
{
name: "names without extension",
input: []string{"curl", "git", "jq", "nonexistent"},
want: []string{"curl", "git", "jq"},
},
{
name: "names with extension",
input: []string{"curl.exe", "git.exe", "jq.exe", "nonexistent.exe"},
want: []string{"curl.exe", "git.exe", "jq.exe"},
},
{
name: "mixed names",
input: []string{"curl", "git.exe", "nonexistent"},
want: []string{"curl", "git.exe"},
},
{
name: "no matches",
input: []string{"foo", "bar"},
want: []string{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := Filter(tc.input)
if len(got) == 0 && len(tc.want) == 0 {
return // Pass: both are empty (nil or empty slice)
}
sort.Strings(got)
sort.Strings(tc.want)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("Filter() = %v, want %v", got, tc.want)
}
})
}
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new test file has no
//go:build windowsguard, so both tests run on non-Windows CI jobs too. Inubuntu-latest(added in.github/workflows/test.yml),ScanDirsuses Unix execute bits, and the test fixtures are created with mode0644, so entries liketool.exeare not treated as executables and the assertions fail. This makes the Linux leg of the matrix fail even though the behavior under test is Windows-specific.Useful? React with 👍 / 👎.