github.com/elves/elvish@v0.15.0/pkg/eval/mods/path/path_test.go (about)

     1  package path
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/elves/elvish/pkg/eval"
     8  	. "github.com/elves/elvish/pkg/eval/evaltest"
     9  	"github.com/elves/elvish/pkg/testutil"
    10  )
    11  
    12  var testDir = testutil.Dir{
    13  	"d1": testutil.Dir{
    14  		"f": testutil.Symlink{filepath.Join("d2", "f")},
    15  		"d2": testutil.Dir{
    16  			"empty": "",
    17  			"f":     "",
    18  			"g":     testutil.Symlink{"f"},
    19  		},
    20  	},
    21  	"s1": testutil.Symlink{filepath.Join("d1", "d2")},
    22  }
    23  
    24  func TestPath(t *testing.T) {
    25  	tmpdir, cleanup := testutil.InTestDir()
    26  	defer cleanup()
    27  	testutil.ApplyDir(testDir)
    28  
    29  	absPath, err := filepath.Abs("a/b/c.png")
    30  	if err != nil {
    31  		panic("unable to convert a/b/c.png to an absolute path")
    32  	}
    33  
    34  	setup := func(ev *eval.Evaler) {
    35  		ev.AddGlobal(eval.NsBuilder{}.AddNs("path", Ns).Ns())
    36  	}
    37  	TestWithSetup(t, setup,
    38  		// This block of tests is not meant to be comprehensive. Their primary purpose is to simply
    39  		// ensure the Elvish command is correctly mapped to the relevant Go function. We assume the
    40  		// Go function behaves correctly.
    41  		That(`path:abs a/b/c.png`).Puts(absPath),
    42  		That(`path:base a/b/d.png`).Puts("d.png"),
    43  		That(`path:clean ././x`).Puts("x"),
    44  		That(`path:clean a/b/.././c`).Puts(filepath.Join("a", "c")),
    45  		That(`path:dir a/b/d.png`).Puts(filepath.Join("a", "b")),
    46  		That(`path:ext a/b/e.png`).Puts(".png"),
    47  		That(`path:ext a/b/s`).Puts(""),
    48  		That(`path:is-abs a/b/s`).Puts(false),
    49  		That(`path:is-abs `+absPath).Puts(true),
    50  		That(`path:eval-symlinks d1/d2`).Puts(filepath.Join("d1", "d2")),
    51  		That(`path:eval-symlinks d1/d2/f`).Puts(filepath.Join("d1", "d2", "f")),
    52  		That(`path:eval-symlinks s1`).Puts(filepath.Join("d1", "d2")),
    53  		That(`path:eval-symlinks d1/f`).Puts(filepath.Join("d1", "d2", "f")),
    54  		That(`path:eval-symlinks s1/g`).Puts(filepath.Join("d1", "d2", "f")),
    55  		That(`path:eval-symlinks s1/empty`).Puts(filepath.Join("d1", "d2", "empty")),
    56  
    57  		// Elvish `path:` module functions that are not trivial wrappers around a Go stdlib function
    58  		// should have comprehensive tests below this comment.
    59  		That(`path:is-dir a/b/s`).Puts(false),
    60  		That(`path:is-dir `+tmpdir).Puts(true),
    61  		That(`path:is-dir s1`).Puts(true),
    62  		That(`path:is-regular a/b/s`).Puts(false),
    63  		That(`path:is-regular `+tmpdir).Puts(false),
    64  		That(`path:is-regular d1/f`).Puts(true),
    65  		That(`path:is-regular d1/d2/f`).Puts(true),
    66  		That(`path:is-regular s1/f`).Puts(true),
    67  	)
    68  }