|
| 1 | +package lsp |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestLoadMixinsStoresAndIndexesMixinDocuments(t *testing.T) { |
| 10 | + dir := t.TempDir() |
| 11 | + |
| 12 | + mainPath := filepath.Join(dir, "lets.yaml") |
| 13 | + baseMixinPath := filepath.Join(dir, "lets.base.yaml") |
| 14 | + localMixinPath := filepath.Join(dir, "lets.local.yaml") |
| 15 | + |
| 16 | + baseMixinDoc := `commands: |
| 17 | + build: |
| 18 | + cmd: echo build` |
| 19 | + |
| 20 | + localMixinDoc := `commands: |
| 21 | + test: |
| 22 | + cmd: echo test` |
| 23 | + |
| 24 | + if err := os.WriteFile(baseMixinPath, []byte(baseMixinDoc), 0o644); err != nil { |
| 25 | + t.Fatalf("WriteFile(%s) error = %v", baseMixinPath, err) |
| 26 | + } |
| 27 | + |
| 28 | + if err := os.WriteFile(localMixinPath, []byte(localMixinDoc), 0o644); err != nil { |
| 29 | + t.Fatalf("WriteFile(%s) error = %v", localMixinPath, err) |
| 30 | + } |
| 31 | + |
| 32 | + mainDoc := `mixins: |
| 33 | + - lets.base.yaml |
| 34 | + - -lets.local.yaml |
| 35 | +commands: |
| 36 | + release: |
| 37 | + depends: [build, test] |
| 38 | + cmd: echo release` |
| 39 | + |
| 40 | + server := &lspServer{ |
| 41 | + storage: newStorage(), |
| 42 | + index: newIndex(logger), |
| 43 | + log: logger, |
| 44 | + } |
| 45 | + |
| 46 | + mainURI := pathToURI(mainPath) |
| 47 | + server.storage.AddDocument(mainURI, mainDoc) |
| 48 | + server.loadMixins(mainURI) |
| 49 | + |
| 50 | + baseMixinURI := pathToURI(baseMixinPath) |
| 51 | + localMixinURI := pathToURI(localMixinPath) |
| 52 | + |
| 53 | + if got := server.storage.GetDocument(baseMixinURI); got == nil || *got != baseMixinDoc { |
| 54 | + t.Fatalf("storage for %s = %#v, want %q", baseMixinURI, got, baseMixinDoc) |
| 55 | + } |
| 56 | + |
| 57 | + if got := server.storage.GetDocument(localMixinURI); got == nil || *got != localMixinDoc { |
| 58 | + t.Fatalf("storage for %s = %#v, want %q", localMixinURI, got, localMixinDoc) |
| 59 | + } |
| 60 | + |
| 61 | + buildInfo, ok := server.index.findCommand("build") |
| 62 | + if !ok { |
| 63 | + t.Fatal("expected build command from mixin to be indexed") |
| 64 | + } |
| 65 | + |
| 66 | + if buildInfo.fileURI != baseMixinURI { |
| 67 | + t.Fatalf("build indexed at %s, want %s", buildInfo.fileURI, baseMixinURI) |
| 68 | + } |
| 69 | + |
| 70 | + testInfo, ok := server.index.findCommand("test") |
| 71 | + if !ok { |
| 72 | + t.Fatal("expected test command from mixin to be indexed") |
| 73 | + } |
| 74 | + |
| 75 | + if testInfo.fileURI != localMixinURI { |
| 76 | + t.Fatalf("test indexed at %s, want %s", testInfo.fileURI, localMixinURI) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestLoadMixinsSkipsMissingFiles(t *testing.T) { |
| 81 | + dir := t.TempDir() |
| 82 | + |
| 83 | + mainPath := filepath.Join(dir, "lets.yaml") |
| 84 | + mainURI := pathToURI(mainPath) |
| 85 | + |
| 86 | + mainDoc := `mixins: |
| 87 | + - missing.yaml |
| 88 | +commands: |
| 89 | + release: |
| 90 | + cmd: echo release` |
| 91 | + |
| 92 | + server := &lspServer{ |
| 93 | + storage: newStorage(), |
| 94 | + index: newIndex(logger), |
| 95 | + log: logger, |
| 96 | + } |
| 97 | + |
| 98 | + server.storage.AddDocument(mainURI, mainDoc) |
| 99 | + server.loadMixins(mainURI) |
| 100 | + |
| 101 | + if got := server.storage.GetDocument(pathToURI(filepath.Join(dir, "missing.yaml"))); got != nil { |
| 102 | + t.Fatalf("expected missing mixin to not be stored, got %#v", got) |
| 103 | + } |
| 104 | + |
| 105 | + if _, ok := server.index.findCommand("missing"); ok { |
| 106 | + t.Fatal("expected no indexed command for missing mixin") |
| 107 | + } |
| 108 | +} |
0 commit comments