|
| 1 | +package session |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGetEnvironmentInfo(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + setupFunc func() string |
| 17 | + expectGit bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "with git repo", |
| 21 | + setupFunc: func() string { |
| 22 | + tmpDir := t.TempDir() |
| 23 | + require.NoError(t, os.Mkdir(filepath.Join(tmpDir, ".git"), 0755)) |
| 24 | + return tmpDir |
| 25 | + }, |
| 26 | + expectGit: true, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "without git repo", |
| 30 | + setupFunc: func() string { |
| 31 | + return t.TempDir() |
| 32 | + }, |
| 33 | + expectGit: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "nonexistent directory", |
| 37 | + setupFunc: func() string { |
| 38 | + return "/path/that/does/not/exist" |
| 39 | + }, |
| 40 | + expectGit: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "empty directory path", |
| 44 | + setupFunc: func() string { |
| 45 | + return "" |
| 46 | + }, |
| 47 | + expectGit: false, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + for _, tt := range tests { |
| 52 | + t.Run(tt.name, func(t *testing.T) { |
| 53 | + dir := tt.setupFunc() |
| 54 | + info := getEnvironmentInfo(dir) |
| 55 | + |
| 56 | + gitStatus := "No" |
| 57 | + if tt.expectGit { |
| 58 | + gitStatus = "Yes" |
| 59 | + } |
| 60 | + |
| 61 | + expected := `Here is useful information about the environment you are running in: |
| 62 | + <env> |
| 63 | + Working directory: ` + dir + ` |
| 64 | + Is directory a git repo: ` + gitStatus + ` |
| 65 | + Platform: ` + runtime.GOOS + ` |
| 66 | + </env>` |
| 67 | + |
| 68 | + assert.Equal(t, expected, info) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestIsGitRepo(t *testing.T) { |
| 74 | + tests := []struct { |
| 75 | + name string |
| 76 | + setupFunc func() string |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "git folder", |
| 80 | + setupFunc: func() string { |
| 81 | + tmpDir := t.TempDir() |
| 82 | + require.NoError(t, os.Mkdir(filepath.Join(tmpDir, ".git"), 0755)) |
| 83 | + return tmpDir |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "git file", |
| 88 | + setupFunc: func() string { |
| 89 | + tmpDir := t.TempDir() |
| 90 | + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, ".git"), []byte("gitdir: /some/other/path"), 0644)) |
| 91 | + return tmpDir |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + for _, tt := range tests { |
| 97 | + t.Run(tt.name, func(t *testing.T) { |
| 98 | + dir := tt.setupFunc() |
| 99 | + assert.True(t, isGitRepo(dir)) |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestIsNotGitRepo(t *testing.T) { |
| 105 | + tests := []struct { |
| 106 | + name string |
| 107 | + setupFunc func() string |
| 108 | + }{ |
| 109 | + { |
| 110 | + name: "no git", |
| 111 | + setupFunc: func() string { |
| 112 | + return t.TempDir() |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "nonexistent directory", |
| 117 | + setupFunc: func() string { |
| 118 | + return "/path/that/does/not/exist" |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "empty path", |
| 123 | + setupFunc: func() string { |
| 124 | + return "" |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + for _, tt := range tests { |
| 130 | + t.Run(tt.name, func(t *testing.T) { |
| 131 | + dir := tt.setupFunc() |
| 132 | + assert.False(t, isGitRepo(dir)) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestBoolToYesNo(t *testing.T) { |
| 138 | + assert.Equal(t, "Yes", boolToYesNo(true)) |
| 139 | + assert.Equal(t, "No", boolToYesNo(false)) |
| 140 | +} |
| 141 | + |
| 142 | +func TestGetEnvironmentInfoIntegration(t *testing.T) { |
| 143 | + wd, err := os.Getwd() |
| 144 | + require.NoError(t, err) |
| 145 | + |
| 146 | + info := getEnvironmentInfo(wd) |
| 147 | + |
| 148 | + assert.Contains(t, info, "Here is useful information about the environment you are running in:") |
| 149 | + assert.Contains(t, info, "<env>") |
| 150 | + assert.Contains(t, info, "</env>") |
| 151 | + assert.Contains(t, info, "Working directory: "+wd) |
| 152 | + assert.Contains(t, info, "Platform: "+runtime.GOOS) |
| 153 | + |
| 154 | + if isGitRepo(wd) { |
| 155 | + assert.Contains(t, info, "Is directory a git repo: Yes") |
| 156 | + } else { |
| 157 | + assert.Contains(t, info, "Is directory a git repo: No") |
| 158 | + } |
| 159 | +} |
0 commit comments