-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recurse the current directory to find imports to guess (#202)
* Recurse the current directory to find imports to guess * ignoredPaths doesn't make sense with fs.WalkDir * Establish a basic depth limit on FS traversal * Avoid unnecessary traversals by just using path.Match * Prepend root to dir * Add tests for tree-sitter directory traversal
- Loading branch information
1 parent
f481a68
commit 46f4bc0
Showing
5 changed files
with
154 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/smacker/go-tree-sitter/python" | ||
) | ||
|
||
var importsQuery = ` | ||
(module | ||
[(import_statement | ||
name: [(dotted_name) @import | ||
(aliased_import | ||
name: (dotted_name) @import)]) | ||
(import_from_statement | ||
module_name: (dotted_name) @import)] | ||
. | ||
(comment)? @pragma) | ||
` | ||
|
||
func writeFile(dir, name string, contents []byte) error { | ||
err := os.MkdirAll(dir, 0o755) | ||
if err != nil { | ||
return err | ||
} | ||
err = os.WriteFile(path.Join(dir, name), contents, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func TestTreeSitter(t *testing.T) { | ||
testDir := t.TempDir() | ||
|
||
expected := map[string]bool{ | ||
"from_root": true, | ||
"from_inner": true, | ||
} | ||
|
||
innerDir := path.Join(testDir, "src", "my_module", "inner") | ||
venvDir := path.Join(testDir, "venv", "ignored_module") | ||
|
||
var err error | ||
err = writeFile(testDir, "root.py", []byte("import from_root")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
err = writeFile(innerDir, "inner.py", []byte("import from_inner")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
err = writeFile(venvDir, "ignored.py", []byte("import from_venv")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
pathSegmentPatterns := []string{"*.py"} | ||
ignorePathSegments := map[string]bool{ | ||
"venv": true, | ||
} | ||
|
||
ctx := context.Background() | ||
py := python.GetLanguage() | ||
foundMap := map[string]bool{} | ||
{ | ||
var found []string | ||
found, err = GuessWithTreeSitter(ctx, testDir, py, importsQuery, pathSegmentPatterns, ignorePathSegments) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
for _, pkg := range found { | ||
foundMap[pkg] = true | ||
} | ||
} | ||
|
||
for pkg := range foundMap { | ||
if expected[pkg] { | ||
delete(expected, pkg) | ||
delete(foundMap, pkg) | ||
} else { | ||
t.Error("Missing match: ", pkg) | ||
} | ||
} | ||
|
||
if len(expected) > 0 { | ||
formatted := []string{} | ||
for pkg := range expected { | ||
formatted = append(formatted, pkg) | ||
} | ||
t.Error("Not all expected checks were passed. Missing:", strings.Join(formatted, ", ")) | ||
} | ||
|
||
if len(foundMap) > 0 { | ||
formatted := []string{} | ||
for pkg := range foundMap { | ||
formatted = append(formatted, pkg) | ||
} | ||
t.Error("Not all expected checks were passed. Extra:", strings.Join(formatted, ", ")) | ||
} | ||
} |