1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
package api
import (
"fmt"
"testing"
"github.com/evanw/esbuild/internal/test"
)
func TestStripDirPrefix(t *testing.T) {
expectSuccess := func(path string, prefix string, allowedSlashes string, expected string) {
t.Helper()
t.Run(fmt.Sprintf("path=%s prefix=%s slashes=%s", path, prefix, allowedSlashes), func(t *testing.T) {
t.Helper()
observed, ok := stripDirPrefix(path, prefix, allowedSlashes)
if !ok {
t.Fatalf("Unexpected failure")
}
test.AssertEqualWithDiff(t, observed, expected)
})
}
expectFailure := func(path string, prefix string, allowedSlashes string) {
t.Helper()
t.Run(fmt.Sprintf("path=%s prefix=%s slashes=%s", path, prefix, allowedSlashes), func(t *testing.T) {
t.Helper()
_, ok := stripDirPrefix(path, prefix, allowedSlashes)
if ok {
t.Fatalf("Unexpected success")
}
})
}
// Note: People sometimes set "outdir" to "/" and expect that to work:
// https://github.com/evanw/esbuild/issues/3027
expectSuccess(`/foo/bar/baz`, ``, `/`, `/foo/bar/baz`)
expectSuccess(`/foo/bar/baz`, `/`, `/`, `foo/bar/baz`)
expectSuccess(`/foo/bar/baz`, `/foo`, `/`, `bar/baz`)
expectSuccess(`/foo/bar/baz`, `/foo/bar`, `/`, `baz`)
expectSuccess(`/foo/bar/baz`, `/foo/bar/baz`, `/`, ``)
expectSuccess(`/foo/bar//baz`, `/foo/bar`, `/`, `/baz`)
expectSuccess(`C:\foo\bar\baz`, ``, `\/`, `C:\foo\bar\baz`)
expectSuccess(`C:\foo\bar\baz`, `C:`, `\/`, `foo\bar\baz`)
expectSuccess(`C:\foo\bar\baz`, `C:\`, `\/`, `foo\bar\baz`)
expectSuccess(`C:\foo\bar\baz`, `C:\foo`, `\/`, `bar\baz`)
expectSuccess(`C:\foo\bar\baz`, `C:\foo\bar`, `\/`, `baz`)
expectSuccess(`C:\foo\bar\baz`, `C:\foo\bar\baz`, `\/`, ``)
expectSuccess(`C:\foo\bar\\baz`, `C:\foo\bar`, `\/`, `\baz`)
expectSuccess(`C:\foo\bar/baz`, `C:\foo\bar`, `\/`, `baz`)
expectFailure(`/foo/bar`, `/foo/ba`, `/`)
expectFailure(`/foo/bar`, `/fo`, `/`)
expectFailure(`C:\foo\bar`, `C:\foo\ba`, `\/`)
expectFailure(`C:\foo\bar`, `C:\fo`, `\/`)
expectFailure(`C:/foo/bar`, `C:\foo`, `\/`)
}
|