package authsession import ( "fmt" "go/parser" "go/token" "io/fs" "path/filepath" "runtime" "strconv" "strings" "testing" "github.com/stretchr/testify/require" ) func TestProductionCoreStaysStorageAgnostic(t *testing.T) { t.Parallel() root := authsessionRootDir(t) for _, relativeDir := range []string{ filepath.Join("internal", "domain"), filepath.Join("internal", "service"), filepath.Join("internal", "ports"), } { checkStorageAgnosticImports(t, filepath.Join(root, relativeDir)) } } func authsessionRootDir(t *testing.T) string { t.Helper() _, thisFile, _, ok := runtime.Caller(0) require.True(t, ok, "runtime.Caller failed") return filepath.Dir(thisFile) } func checkStorageAgnosticImports(t *testing.T, dir string) { t.Helper() fileSet := token.NewFileSet() err := filepath.WalkDir(dir, func(path string, entry fs.DirEntry, walkErr error) error { if walkErr != nil { return walkErr } if entry.IsDir() { return nil } if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { return nil } file, err := parser.ParseFile(fileSet, path, nil, parser.ImportsOnly) if err != nil { return err } for _, importSpec := range file.Imports { importPath, err := strconv.Unquote(importSpec.Path.Value) if err != nil { return err } if importPath == "github.com/redis/go-redis/v9" || strings.Contains(importPath, "internal/adapters/redis") { return fmt.Errorf("storage-specific import %q found in %s", importPath, path) } } return nil }) require.NoError(t, err) }