Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions pkg/linter/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package linter

import (
"os"
"path/filepath"
"testing"
)

func TestLintModuleImports(t *testing.T) {
for _, tc := range []struct {
name, content string
wantLine int
}{
{
name: "standalone missing import",
content: "# Module\n<!-- cheat\nexport local\nimport missing\nvar x := hello\n-->\n",
wantLine: 4,
},
{
name: "standalone valid import",
content: "# Dependency\n<!-- cheat\nexport dependency\nvar x := hello\n-->\n" +
"# Module\n<!-- cheat\nexport local\nimport dependency\n-->\n",
},
{
name: "ordinary cheat missing import",
content: "# Command\n```sh\necho hello\n```\n<!-- cheat\nimport missing\n-->\n",
wantLine: 6,
},
{
name: "exported cheat missing import reported once",
content: "# Command\n```sh\necho hello\n```\n<!-- cheat\nexport local\nimport missing\n-->\n",
wantLine: 7,
},
} {
for _, mode := range []string{"file", "directory"} {
t.Run(tc.name+"/"+mode, func(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "imports.md")
if err := os.WriteFile(path, []byte(tc.content), 0600); err != nil {
t.Fatal(err)
}
target := path
if mode == "directory" {
target = dir
}
findings, err := Lint(target)
if err != nil {
t.Fatal(err)
}
if tc.wantLine == 0 {
if len(findings) != 0 {
t.Fatalf("valid import produced findings: %+v", findings)
}
return
}
want := Finding{File: path, Line: tc.wantLine, Column: 8, Severity: SeverityError, Message: `import "missing" does not resolve to any exported module`}
if len(findings) != 1 || findings[0] != want {
t.Fatalf("Lint() = %+v, want [%+v]", findings, want)
}
})
}
}
}
10 changes: 10 additions & 0 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,19 @@ func checkMissingChainSteps(name string, steps map[int]*parser.Cheat) []Finding

func lintImports(index *parser.CheatIndex) []Finding {
var findings []Finding
seen := make(map[*parser.Cheat]bool)
for _, c := range index.Cheats {
seen[c] = true
findings = append(findings, lintCheatImports(c, index)...)
}
for _, module := range index.Modules {
for _, c := range module.Cheats {
if !seen[c] {
seen[c] = true
findings = append(findings, lintCheatImports(c, index)...)
}
}
}
return findings
}

Expand Down
Loading