vet/pkg/code/languages/python_test.go
abhisek e6f6288701
feat: Code analysis framework infra
feat: Building code graph

Refactor to support import processing

Handle relative import name fixup

Add docs for code analysis framework

Update docs to include additional examples

feat: Function call graph

Update code graph to link function decl and calls

Include call node in function calls

feat: Flatten vulnerabilities in CSV reporter

refactor: Maintain separation of concerns for code analysis framework

refactor: Separate storage entities in its own package

feat: Add callback support in code graph builder

docs: Fix code analysis framework docs
Signed-off-by: abhisek <abhisek.datta@gmail.com>
2024-07-11 15:09:11 +05:30

57 lines
1.1 KiB
Go

package languages
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestResolveImportNameFromPath(t *testing.T) {
cases := []struct {
name string
path string
expected string
err error
}{
{
name: "Path is module",
path: "module.py",
expected: "module",
err: nil,
},
{
name: "Path is module with subdirectory",
path: "subdir/module.py",
expected: "subdir.module",
err: nil,
},
{
name: "Path is module with subdirectory and init",
path: "subdir/__init__.py",
expected: "subdir",
err: nil,
},
{
name: "Absolute path",
path: "/subdir/module.py",
expected: "",
err: fmt.Errorf("path is not relative: /subdir/module.py"),
},
}
l := &pythonSourceLanguage{}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
res, err := l.ResolveImportNameFromPath(test.path)
if test.err != nil {
assert.ErrorContains(t, err, test.err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, test.expected, res)
}
})
}
}