loading_test.go

 1package execenv
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8
 9	"github.com/stretchr/testify/assert"
10	"github.com/stretchr/testify/require"
11)
12
13// ***** WARNING ***** - due to the use of testing with an environment
14//
15//	variable, do NOT use t.Parallel() with these
16//	test cases.
17func TestGetRepoPath(t *testing.T) {
18	t.Run("no Git path provided (use WD)", func(t *testing.T) {
19		// WD during tests is the directory containing the test file
20		path, err := getRepoPath(&Env{})
21		require.NoError(t, err)
22		assert.True(t, strings.HasSuffix(path, filepath.Join("commands", "execenv")))
23	})
24
25	t.Run("flag provided", func(t *testing.T) {
26		gitDir := filepath.Join(string(filepath.Separator), "tmp")
27
28		path, err := getRepoPath(&Env{
29			GitDir: gitDir,
30		})
31		require.NoError(t, err)
32		assert.Equal(t, gitDir, path)
33	})
34
35	t.Run("environment variable provided", func(t *testing.T) {
36		gitDir := filepath.Join(string(filepath.Separator), "tmp")
37		t.Setenv("GIT_DIR", gitDir)
38
39		path, err := getRepoPath(&Env{})
40		require.NoError(t, err)
41		assert.Equal(t, gitDir, path)
42	})
43
44	t.Run("GIT_DIR supercedes --git-dir", func(t *testing.T) {
45		homeDir, err := os.UserHomeDir()
46		require.NoError(t, err)
47
48		tmpDir := filepath.Join(string(filepath.Separator), "tmp")
49		t.Setenv("GIT_DIR", tmpDir)
50
51		path, err := getRepoPath(&Env{
52			GitDir: homeDir,
53		})
54		require.NoError(t, err)
55		assert.Equal(t, tmpDir, path)
56
57	})
58
59	t.Run("path does not exist", func(t *testing.T) {
60		_, err := getRepoPath(&Env{
61			GitDir: filepath.Join("/", "tmp", "does-not-exist"),
62		})
63		require.ErrorIs(t, err, os.ErrNotExist)
64	})
65
66	t.Run("path is not a directory", func(t *testing.T) {
67		gitDir := filepath.Join(os.TempDir(), "plain-file")
68
69		f, err := os.Create(gitDir)
70		require.NoError(t, err)
71		require.NoError(t, f.Close())
72
73		_, err = getRepoPath(&Env{
74			GitDir: gitDir,
75		})
76		require.ErrorIs(t, err, errNotDirectory)
77	})
78}