Skip to content
Open
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
20 changes: 8 additions & 12 deletions internal/filepathext/filepathext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package filepathext
import (
"os"
"path/filepath"
"strings"
"regexp"
"slices"
)

// SmartJoin joins two paths, but only if the second is not already an
Expand All @@ -25,19 +26,14 @@ func IsAbs(path string) bool {
return filepath.IsAbs(path)
}

var knownAbsDirs = []string{
".ROOT_DIR",
".TASKFILE_DIR",
".USER_WORKING_DIR",
}
var (
templateActionRE = regexp.MustCompile(`\{\{[^{}]*\}\}`)
// Use a Unicode-aware boundary so longer variable names remain relative.
specialDirRE = regexp.MustCompile(`\.(ROOT_DIR|TASKFILE_DIR|USER_WORKING_DIR)([^\pL\p{Nd}_]|$)`)
)

func isSpecialDir(dir string) bool {
for _, d := range knownAbsDirs {
if strings.Contains(dir, d) {
return true
}
}
return false
return slices.ContainsFunc(templateActionRE.FindAllString(dir, -1), specialDirRE.MatchString)
}

// TryAbsToRel tries to convert an absolute path to relative based on the
Expand Down
47 changes: 47 additions & 0 deletions internal/filepathext/filepathext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package filepathext

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestIsAbs(t *testing.T) {
t.Parallel()

absolute, err := filepath.Abs("file.txt")
require.NoError(t, err)

for _, test := range []struct {
path string
wantAbs bool
}{
{"", false},
{"project.ROOT_DIR/file.txt", false},
{".TASKFILE_DIR/file.txt", false},
{"project.USER_WORKING_DIR/file.txt", false},
{"{{.PROJECT}}.ROOT_DIR{{.SUFFIX}}/file.txt", false},
{"{{ .ROOT_DIR/file.txt", false},
{"{{.PROJECT}}/file.txt", false},
{"{{.ROOT_DIR_SUFFIX}}/file.txt", false},
{"{{.ROOT_DIR2}}/file.txt", false},
{"{{.ROOT_DIRsuffix}}/file.txt", false},
{"{{.ROOT_DIR变量}}/file.txt", false},
{absolute, true},
{"{{.ROOT_DIR}}/file.txt", true},
{"{{ .TASKFILE_DIR }}/file.txt", true},
{"{{- .USER_WORKING_DIR -}}/file.txt", true},
{"{{.ROOT_DIR|toSlash}}/file.txt", true},
{"{{(.ROOT_DIR)}}/file.txt", true},
{"{{.PROJECT}}/{{.ROOT_DIR}}/file.txt", true},
{"{{\n.TASKFILE_DIR\n}}/file.txt", true},
{`{{joinPath .ROOT_DIR "src"}}/file.txt`, true},
} {
t.Run(test.path, func(t *testing.T) {
t.Parallel()

require.Equal(t, test.wantAbs, IsAbs(test.path))
})
}
}
Comment on lines +1 to +47

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many tests for a single regex.
There are 4 functions and about 40 subcases, while SmartJoin only calls IsAbs.

  • A single {path, wantAbs} table on IsAbs would cover everything.
  • t.TempDir() isn't needed for base since nothing touches the disk: a constant is enough.
  • TestSmartJoinUnrelatedTemplatePaths already passed before this PR, so it can be folded into the table