|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestFindKoolYamlPrefersYml(t *testing.T) { |
| 11 | + dir := t.TempDir() |
| 12 | + for _, name := range []string{"kool.yml", "kool.yaml"} { |
| 13 | + if err := os.WriteFile(filepath.Join(dir, name), []byte("scripts: {}\n"), 0644); err != nil { |
| 14 | + t.Fatal(err) |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + file, err := FindKoolYaml(dir) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("unexpected error: %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + if expected := filepath.Join(dir, "kool.yml"); file != expected { |
| 24 | + t.Errorf("expected %q, got %q", expected, file) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestFindKoolYamlFallsBackToYaml(t *testing.T) { |
| 29 | + dir := t.TempDir() |
| 30 | + if err := os.WriteFile(filepath.Join(dir, "kool.yaml"), []byte("scripts: {}\n"), 0644); err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + |
| 34 | + file, err := FindKoolYaml(dir) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("unexpected error: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + if expected := filepath.Join(dir, "kool.yaml"); file != expected { |
| 40 | + t.Errorf("expected %q, got %q", expected, file) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestFindKoolYamlNotFound(t *testing.T) { |
| 45 | + if _, err := FindKoolYaml(t.TempDir()); !errors.Is(err, ErrKoolYmlNotFound) { |
| 46 | + t.Errorf("expected ErrKoolYmlNotFound, got %v", err) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestLoadKoolYaml(t *testing.T) { |
| 51 | + dir := t.TempDir() |
| 52 | + if err := os.WriteFile(filepath.Join(dir, "kool.yml"), []byte("workspaces:\n - app\n"), 0644); err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + |
| 56 | + parsed, err := LoadKoolYaml(dir) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("unexpected error: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + if len(parsed.Workspaces) != 1 || parsed.Workspaces[0] != "app" { |
| 62 | + t.Errorf("expected workspaces [app], got %v", parsed.Workspaces) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestLoadKoolYamlMissing(t *testing.T) { |
| 67 | + if _, err := LoadKoolYaml(t.TempDir()); !errors.Is(err, ErrKoolYmlNotFound) { |
| 68 | + t.Errorf("expected ErrKoolYmlNotFound, got %v", err) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// A malformed config must be distinguishable from a missing one, so callers |
| 73 | +// can choose to report it rather than silently behaving as if unconfigured. |
| 74 | +func TestLoadKoolYamlMalformed(t *testing.T) { |
| 75 | + dir := t.TempDir() |
| 76 | + if err := os.WriteFile(filepath.Join(dir, "kool.yml"), []byte("scripts: [oops\n"), 0644); err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + |
| 80 | + _, err := LoadKoolYaml(dir) |
| 81 | + if err == nil { |
| 82 | + t.Fatal("expected a decoding error, got none") |
| 83 | + } |
| 84 | + |
| 85 | + if errors.Is(err, ErrKoolYmlNotFound) { |
| 86 | + t.Error("malformed config reported as not found") |
| 87 | + } |
| 88 | +} |
0 commit comments