|
1 | 1 | package lint
|
2 | 2 |
|
3 |
| -import "testing" |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRetrieveModFile(t *testing.T) { |
| 10 | + t.Run("go.mod file exists", func(t *testing.T) { |
| 11 | + nestedDir := filepath.Join(t.TempDir(), "nested", "dir", "structure") |
| 12 | + err := os.MkdirAll(nestedDir, 0o755) |
| 13 | + if err != nil { |
| 14 | + t.Fatal(err) |
| 15 | + } |
| 16 | + |
| 17 | + modFilePath := filepath.Join(nestedDir, "go.mod") |
| 18 | + err = os.WriteFile(modFilePath, []byte("module example.com/test"), 0o644) |
| 19 | + if err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + foundPath, err := retrieveModFile(nestedDir) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("unexpected error: %v", err) |
| 25 | + } |
| 26 | + if foundPath != modFilePath { |
| 27 | + t.Fatalf("expected %q, got %q", modFilePath, foundPath) |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + t.Run("go.mod file does not exist", func(t *testing.T) { |
| 32 | + _, err := retrieveModFile(t.TempDir()) |
| 33 | + if err == nil { |
| 34 | + t.Fatalf("expected error, got nil") |
| 35 | + } |
| 36 | + expectedErrMsg := `did not found "go.mod" file` |
| 37 | + if err.Error() != expectedErrMsg { |
| 38 | + t.Fatalf("expected error message %q, got %q", expectedErrMsg, err.Error()) |
| 39 | + } |
| 40 | + }) |
| 41 | +} |
4 | 42 |
|
5 | 43 | // TestIsGenerated tests isGenerated function.
|
6 | 44 | func TestIsGenerated(t *testing.T) { //revive:disable-line:exported
|
|
0 commit comments