File: chdir_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (64 lines) | stat: -rw-r--r-- 1,278 bytes parent folder | download
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
58
59
60
61
62
63
64
package eval_test

import (
	"os"
	"testing"

	"src.elv.sh/pkg/env"
	. "src.elv.sh/pkg/eval"
	"src.elv.sh/pkg/must"
	"src.elv.sh/pkg/testutil"
)

func TestChdir(t *testing.T) {
	dst := testutil.TempDir(t)

	ev := NewEvaler()

	argDirInBefore, argDirInAfter := "", ""
	ev.BeforeChdir = append(ev.BeforeChdir, func(dir string) { argDirInBefore = dir })
	ev.AfterChdir = append(ev.AfterChdir, func(dir string) { argDirInAfter = dir })

	back := saveWd()
	defer back()

	err := ev.Chdir(dst)

	if err != nil {
		t.Errorf("Chdir => error %v", err)
	}
	if envPwd := os.Getenv(env.PWD); envPwd != dst {
		t.Errorf("$PWD is %q after Chdir, want %q", envPwd, dst)
	}

	if argDirInBefore != dst {
		t.Errorf("Chdir called before-hook with %q, want %q",
			argDirInBefore, dst)
	}
	if argDirInAfter != dst {
		t.Errorf("Chdir called before-hook with %q, want %q",
			argDirInAfter, dst)
	}
}

func TestChdirError(t *testing.T) {
	testutil.InTempDir(t)

	ev := NewEvaler()
	err := ev.Chdir("i/dont/exist")
	if err == nil {
		t.Errorf("Chdir => no error when dir does not exist")
	}
}

// Saves the current working directory, and returns a function for returning to
// it.
func saveWd() func() {
	wd, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	return func() {
		must.Chdir(wd)
	}
}