|
| 1 | +// Copyright IBM Corp. 2020, 2026 |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package jobs |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/go-version" |
| 12 | + lsctx "github.com/hashicorp/terraform-ls/internal/context" |
| 13 | + "github.com/hashicorp/terraform-ls/internal/features/policy/ast" |
| 14 | + "github.com/hashicorp/terraform-ls/internal/features/policy/state" |
| 15 | + "github.com/hashicorp/terraform-ls/internal/filesystem" |
| 16 | + ilsp "github.com/hashicorp/terraform-ls/internal/lsp" |
| 17 | + globalState "github.com/hashicorp/terraform-ls/internal/state" |
| 18 | + globalAst "github.com/hashicorp/terraform-ls/internal/terraform/ast" |
| 19 | + "github.com/hashicorp/terraform-ls/internal/uri" |
| 20 | + tfmod "github.com/hashicorp/terraform-schema/module" |
| 21 | +) |
| 22 | + |
| 23 | +// --- Mocks & Helpers --- |
| 24 | + |
| 25 | +type PolicyRootReaderMock struct{} |
| 26 | + |
| 27 | +func (r PolicyRootReaderMock) InstalledModuleCalls(modPath string) (map[string]tfmod.InstalledModuleCall, error) { |
| 28 | + return nil, nil |
| 29 | +} |
| 30 | +func (r PolicyRootReaderMock) TerraformVersion(modPath string) *version.Version { |
| 31 | + return nil |
| 32 | +} |
| 33 | +func (r PolicyRootReaderMock) InstalledModulePath(rootPath string, normalizedSource string) (string, bool) { |
| 34 | + return "", false |
| 35 | +} |
| 36 | + |
| 37 | +func setupTestEnv(t *testing.T) (*state.PolicyStore, *filesystem.Filesystem, string) { |
| 38 | + gs, err := globalState.NewStateStore() |
| 39 | + if err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + |
| 43 | + ps, err := state.NewPolicyStore(gs.ChangeStore) |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + testData, err := filepath.Abs("testdata") |
| 49 | + if err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + |
| 53 | + policyPath := filepath.Join(testData, "simple-policy") |
| 54 | + err = ps.Add(policyPath) |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + fs := filesystem.NewFilesystem(gs.DocumentStore) |
| 60 | + return ps, fs, policyPath |
| 61 | +} |
| 62 | + |
| 63 | +func TestParsePolicy_FullPolicy(t *testing.T) { |
| 64 | + ps, fs, policyPath := setupTestEnv(t) |
| 65 | + ctx := lsctx.WithDocumentContext(context.Background(), lsctx.Document{ |
| 66 | + Method: "textDocument/didOpen", |
| 67 | + LanguageID: ilsp.Policy.String(), |
| 68 | + }) |
| 69 | + |
| 70 | + // Run Parse |
| 71 | + if err := ParsePolicyConfiguration(ctx, fs, ps, policyPath); err != nil { |
| 72 | + t.Fatalf("Full parse failed: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + // Run Validation |
| 76 | + if err := SchemaPolicyValidation(ctx, ps, PolicyRootReaderMock{}, policyPath); err != nil { |
| 77 | + t.Fatalf("Full schema validation failed: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + record, _ := ps.PolicyRecordByPath(policyPath) |
| 81 | + if len(record.ParsedPolicyFiles) == 0 { |
| 82 | + t.Error("Expected files to be parsed, but map is empty") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestParsePolicy_SingleFile(t *testing.T) { |
| 87 | + ps, fs, policyPath := setupTestEnv(t) |
| 88 | + |
| 89 | + mainFile := "config.policy.hcl" |
| 90 | + absPath, _ := filepath.Abs(filepath.Join(policyPath, mainFile)) |
| 91 | + mainURI := uri.FromPath(absPath) |
| 92 | + |
| 93 | + ctx := lsctx.WithDocumentContext(context.Background(), lsctx.Document{ |
| 94 | + Method: "textDocument/didChange", |
| 95 | + LanguageID: ilsp.Policy.String(), |
| 96 | + URI: mainURI, |
| 97 | + }) |
| 98 | + |
| 99 | + if err := ParsePolicyConfiguration(ctx, fs, ps, policyPath); err != nil { |
| 100 | + t.Fatalf("Incremental parse failed: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + if err := SchemaPolicyValidation(ctx, ps, PolicyRootReaderMock{}, policyPath); err != nil { |
| 104 | + t.Fatalf("Incremental schema validation failed: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + record, _ := ps.PolicyRecordByPath(policyPath) |
| 108 | + filename := ast.PolicyFilename(mainFile) |
| 109 | + |
| 110 | + if _, ok := record.PolicyDiagnostics[globalAst.SchemaValidationSource][filename]; !ok { |
| 111 | + t.Errorf("Diagnostic for %s missing from store", filename) |
| 112 | + } |
| 113 | +} |
0 commit comments