snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
This commit is contained in:
66
internal/sql/sqlpath/read.go
Normal file
66
internal/sql/sqlpath/read.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package sqlpath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/sqlc-dev/sqlc/internal/migrations"
|
||||
)
|
||||
|
||||
// Return a list of SQL files in the listed paths.
|
||||
//
|
||||
// Only includes files ending in .sql. Omits hidden files, directories, and
|
||||
// down migrations.
|
||||
|
||||
// If a path contains *, ?, [, or ], treat the path as a pattern and expand it
|
||||
// filepath.Glob.
|
||||
func Glob(patterns []string) ([]string, error) {
|
||||
var files, paths []string
|
||||
for _, pattern := range patterns {
|
||||
if strings.ContainsAny(pattern, "*?[]") {
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// if len(matches) == 0 {
|
||||
// slog.Warn("zero files matched", "pattern", pattern)
|
||||
// }
|
||||
paths = append(paths, matches...)
|
||||
} else {
|
||||
paths = append(paths, pattern)
|
||||
}
|
||||
}
|
||||
for _, path := range paths {
|
||||
f, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("path error: %w", err)
|
||||
}
|
||||
if f.IsDir() {
|
||||
listing, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, f := range listing {
|
||||
files = append(files, filepath.Join(path, f.Name()))
|
||||
}
|
||||
} else {
|
||||
files = append(files, filepath.Clean(path))
|
||||
}
|
||||
}
|
||||
var sqlFiles []string
|
||||
for _, file := range files {
|
||||
if !strings.HasSuffix(file, ".sql") {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(filepath.Base(file), ".") {
|
||||
continue
|
||||
}
|
||||
if migrations.IsDown(filepath.Base(file)) {
|
||||
continue
|
||||
}
|
||||
sqlFiles = append(sqlFiles, file)
|
||||
}
|
||||
return sqlFiles, nil
|
||||
}
|
||||
243
internal/sql/sqlpath/read_test.go
Normal file
243
internal/sql/sqlpath/read_test.go
Normal file
@@ -0,0 +1,243 @@
|
||||
package sqlpath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
// Returns a list of SQL files from given paths.
|
||||
func TestReturnsListOfSQLFiles(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"testdata/file1.sql", "testdata/file2.sql"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
expected := []string{filepath.Join("testdata", "file1.sql"), filepath.Join("testdata", "file2.sql")}
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v, %v", expected, result, cmp.Diff(expected, result))
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReturnsNilListWhenNoSQLFilesFound(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"testdata/extra.txt"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
// Assert
|
||||
var expected []string
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v, %v", expected, result, cmp.Diff(expected, result))
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIgnoresHiddenFilesWhenSearchingForSQLFiles(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"testdata/.hidden.sql"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
var expected []string
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIgnoresNonSQLFilesWhenSearchingForSQLFiles(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"testdata/extra.txt"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
var expected []string
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExcludesSQLFilesEndingWithDownSQLWhenSearchingForSQLFiles(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"testdata/file1.sql", "testdata/file3.down.sql"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
expected := []string{filepath.Join("testdata", "file1.sql")}
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReturnsErrorWhenPathDoesNotExist(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"non_existent_path"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
var expected []string
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
}
|
||||
if err == nil {
|
||||
t.Errorf("Expected an error, but got nil")
|
||||
} else {
|
||||
expectedError := fmt.Errorf("path error:")
|
||||
if !strings.HasPrefix(err.Error(), expectedError.Error()) {
|
||||
t.Errorf("Expected error %v, but got %v", expectedError, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReturnsErrorWhenDirectoryCannotBeRead(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"testdata/unreadable"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
var expected []string
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
}
|
||||
if err == nil {
|
||||
t.Errorf("Expected an error, but got nil")
|
||||
} else {
|
||||
expectedError := fmt.Errorf("path error:")
|
||||
if !strings.HasPrefix(err.Error(), expectedError.Error()) {
|
||||
t.Errorf("Expected error %v, but got %v", expectedError, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoesNotIncludesSQLFilesWithUppercaseExtension(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"testdata/file4.SQL"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
var expected []string
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotIncludesHiddenFilesAnyPath(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{
|
||||
"./testdata/.hiddendir/file1.sql", // pass
|
||||
"./testdata/.hidden.sql", // skip
|
||||
}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
expected := []string{filepath.Join("testdata", ".hiddendir", "file1.sql")}
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFollowSymlinks(t *testing.T) {
|
||||
// Arrange
|
||||
paths := []string{"testdata/symlink", "testdata/file1.symlink.sql"}
|
||||
|
||||
// Act
|
||||
result, err := Glob(paths)
|
||||
|
||||
// Assert
|
||||
expected := []string{
|
||||
filepath.Join("testdata", "symlink", "file1.sql"),
|
||||
filepath.Join("testdata", "symlink", "file1.symlink.sql"),
|
||||
filepath.Join("testdata", "symlink", "file2.sql"),
|
||||
filepath.Join("testdata", "file1.symlink.sql"),
|
||||
}
|
||||
if !cmp.Equal(result, expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, result)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobPattern(t *testing.T) {
|
||||
// Arrange
|
||||
tests := []struct {
|
||||
pattern string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
pattern: "testdata/glob/*/queries",
|
||||
expected: []string{
|
||||
filepath.Join("testdata", "glob", "sub1", "queries", "file1.sql"),
|
||||
filepath.Join("testdata", "glob", "sub2", "queries", "file2.sql"),
|
||||
filepath.Join("testdata", "glob", "sub3", "queries", "file3.sql"),
|
||||
filepath.Join("testdata", "glob", "sub3", "queries", "file4.sql"),
|
||||
},
|
||||
},
|
||||
{
|
||||
pattern: "testdata/glob/sub3/queries/file?.sql",
|
||||
expected: []string{
|
||||
filepath.Join("testdata", "glob", "sub3", "queries", "file3.sql"),
|
||||
filepath.Join("testdata", "glob", "sub3", "queries", "file4.sql"),
|
||||
},
|
||||
},
|
||||
{
|
||||
pattern: "testdata/glob/sub3/queries/file[1-5].sql",
|
||||
expected: []string{
|
||||
filepath.Join("testdata", "glob", "sub3", "queries", "file3.sql"),
|
||||
filepath.Join("testdata", "glob", "sub3", "queries", "file4.sql"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
// Act
|
||||
result, err := Glob([]string{test.pattern})
|
||||
|
||||
// Assert
|
||||
if !cmp.Equal(result, test.expected) {
|
||||
t.Errorf("Pattern %v: Expected %v, but got %v", test.pattern, test.expected, result)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Pattern %v: Expected no error, but got %v", test.pattern, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
0
internal/sql/sqlpath/testdata/.hidden.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/.hidden.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/.hiddendir/file1.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/.hiddendir/file1.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/extra.txt
vendored
Normal file
0
internal/sql/sqlpath/testdata/extra.txt
vendored
Normal file
0
internal/sql/sqlpath/testdata/file1.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/file1.sql
vendored
Normal file
1
internal/sql/sqlpath/testdata/file1.symlink.sql
vendored
Symbolic link
1
internal/sql/sqlpath/testdata/file1.symlink.sql
vendored
Symbolic link
@@ -0,0 +1 @@
|
||||
./file1.sql
|
||||
0
internal/sql/sqlpath/testdata/file2.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/file2.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/file3.down.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/file3.down.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/file4.SQL
vendored
Normal file
0
internal/sql/sqlpath/testdata/file4.SQL
vendored
Normal file
0
internal/sql/sqlpath/testdata/glob/sub1/queries/file1.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/glob/sub1/queries/file1.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/glob/sub2/queries/file2.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/glob/sub2/queries/file2.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/glob/sub3/queries/file3.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/glob/sub3/queries/file3.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/glob/sub3/queries/file4.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/glob/sub3/queries/file4.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/subdir/file2.sql
vendored
Normal file
0
internal/sql/sqlpath/testdata/subdir/file2.sql
vendored
Normal file
1
internal/sql/sqlpath/testdata/symlink
vendored
Symbolic link
1
internal/sql/sqlpath/testdata/symlink
vendored
Symbolic link
@@ -0,0 +1 @@
|
||||
.
|
||||
Reference in New Issue
Block a user