MCPcopy Index your code
hub / github.com/jetify-com/devbox / testWriteDevboxShellrc

Function testWriteDevboxShellrc

internal/devbox/shell_test.go:36–116  ·  view source on GitHub ↗
(t *testing.T, testdirs []string)

Source from the content-addressed store, hash-verified

34}
35
36func testWriteDevboxShellrc(t *testing.T, testdirs []string) {
37 projectDir := "/path/to/projectDir"
38
39 // Load up all the necessary data from each internal/nix/testdata/shellrc directory
40 // into a slice of tests cases.
41 tests := make([]struct {
42 name string
43 env map[string]string
44 hooksFilePath string
45 shellrcPath string
46 goldShellrcPath string
47 goldShellrc []byte
48 }, len(testdirs))
49 var err error
50 for i, path := range testdirs {
51 test := &tests[i]
52 test.name = filepath.Base(path)
53 if b, err := os.ReadFile(filepath.Join(path, "env")); err == nil {
54 test.env = envir.PairsToMap(strings.Split(string(b), "\n"))
55 }
56
57 test.hooksFilePath = shellgen.ScriptPath(projectDir, shellgen.HooksFilename)
58
59 test.shellrcPath = filepath.Join(path, "shellrc")
60 if _, err := os.Stat(test.shellrcPath); errors.Is(err, fs.ErrNotExist) {
61 test.shellrcPath = ""
62 }
63 test.goldShellrcPath = filepath.Join(path, "shellrc.golden")
64 test.goldShellrc, err = os.ReadFile(test.goldShellrcPath)
65 if err != nil {
66 t.Fatal("Got error reading golden file:", err)
67 }
68 }
69
70 for _, test := range tests {
71 t.Run(test.name, func(t *testing.T) {
72 s := &DevboxShell{
73 devbox: &Devbox{projectDir: projectDir},
74 env: test.env,
75 projectDir: "/path/to/projectDir",
76 userShellrcPath: test.shellrcPath,
77 }
78 // Set shell name based on test name for zsh tests
79 if strings.Contains(test.name, "zsh") {
80 s.name = shZsh
81 }
82 gotPath, err := s.writeDevboxShellrc()
83 if err != nil {
84 t.Fatal("Got writeDevboxShellrc error:", err)
85 }
86 gotShellrc, err := os.ReadFile(gotPath)
87 if err != nil {
88 t.Fatalf("Got error reading generated shellrc at %s: %v", gotPath, err)
89 }
90
91 // Overwrite the golden file if the -update flag was
92 // set, and then proceed normally. The test should
93 // always pass in this case.

Callers 1

TestWriteDevboxShellrcFunction · 0.85

Calls 6

writeDevboxShellrcMethod · 0.95
PairsToMapFunction · 0.92
ScriptPathFunction · 0.92
IsMethod · 0.80
RunMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected