|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package linter_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/mendixlabs/mxcli/mdl/catalog" |
| 12 | + "github.com/mendixlabs/mxcli/mdl/linter" |
| 13 | +) |
| 14 | + |
| 15 | +const xpathTestRule = ` |
| 16 | +RULE_ID = "PERF001" |
| 17 | +RULE_NAME = "xpath builtins" |
| 18 | +DESCRIPTION = "exercises xpath_expressions and parse_xpath builtins" |
| 19 | +CATEGORY = "performance" |
| 20 | +SEVERITY = "info" |
| 21 | +
|
| 22 | +def check(): |
| 23 | + out = [] |
| 24 | + for e in xpath_expressions(): |
| 25 | + ast = parse_xpath(e.xpath_expression) |
| 26 | + out.append(violation( |
| 27 | + message = "entry %s kind %s" % (e.document_qualified_name, ast.kind), |
| 28 | + )) |
| 29 | + return out |
| 30 | +` |
| 31 | + |
| 32 | +func TestStarlarkXPathBuiltins(t *testing.T) { |
| 33 | + // Use a file-based catalog: an in-memory one pools separate connections, so |
| 34 | + // inserts on one aren't visible to queries on another. |
| 35 | + cat, err := catalog.NewFromFile(filepath.Join(t.TempDir(), "cat.db")) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("catalog.NewFromFile: %v", err) |
| 38 | + } |
| 39 | + defer cat.Close() |
| 40 | + db := cat.CatalogDB() |
| 41 | + |
| 42 | + // One retrieve action in module MyApp with a not(…) XPath. |
| 43 | + if _, err := db.Exec( |
| 44 | + `INSERT INTO xpath_expressions_data |
| 45 | + (Id, DocumentType, DocumentId, DocumentQualifiedName, |
| 46 | + ComponentType, ComponentId, XPathExpression, |
| 47 | + IsParameterized, UsageType, ModuleName, ProjectId, SnapshotId) |
| 48 | + VALUES ('x1','MICROFLOW','mf-1','MyApp.GetActiveItems', |
| 49 | + 'RETRIEVE_ACTION','ra-1','[not(Status = ''MyApp.Status.Active'')]', |
| 50 | + 0,'RETRIEVE','MyApp','default','s1')`); err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + dir := t.TempDir() |
| 55 | + path := filepath.Join(dir, "xpath_test.star") |
| 56 | + if err := os.WriteFile(path, []byte(xpathTestRule), 0644); err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + |
| 60 | + rule, err := linter.LoadStarlarkRule(path) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("LoadStarlarkRule: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + ctx := linter.NewLintContext(cat, nil) |
| 66 | + violations := rule.Check(ctx) |
| 67 | + |
| 68 | + var msgs []string |
| 69 | + for _, v := range violations { |
| 70 | + msgs = append(msgs, v.Message) |
| 71 | + } |
| 72 | + joined := strings.Join(msgs, "\n") |
| 73 | + |
| 74 | + for _, want := range []string{ |
| 75 | + "entry MyApp.GetActiveItems", // xpath_expressions() returned the row |
| 76 | + "kind unary", // parse_xpath() parsed not(…) as a unary node |
| 77 | + } { |
| 78 | + if !strings.Contains(joined, want) { |
| 79 | + t.Errorf("missing %q in violations:\n%s", want, joined) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments