@@ -21,6 +21,68 @@ import (
2121 "github.com/stretchr/testify/require"
2222)
2323
24+ func Test_Os_GetExecutionDir (t * testing.T ) {
25+ tests := map [string ]struct {
26+ executionDirPathAbs string
27+ expectedExecutionDirPathAbs string
28+ }{
29+ "When unset should return blank" : {
30+ executionDirPathAbs : "" ,
31+ expectedExecutionDirPathAbs : "" ,
32+ },
33+ "When set should return path" : {
34+ executionDirPathAbs : "/path/to/execution/dir" ,
35+ expectedExecutionDirPathAbs : "/path/to/execution/dir" ,
36+ },
37+ }
38+ for name , tc := range tests {
39+ t .Run (name , func (t * testing.T ) {
40+ // Setup
41+ os := NewOs ()
42+ os .executionDirPathAbs = tc .executionDirPathAbs
43+
44+ // Run the test
45+ actualExecutionDirPathAbs := os .GetExecutionDir ()
46+
47+ // Assertions
48+ require .Equal (t , tc .expectedExecutionDirPathAbs , actualExecutionDirPathAbs )
49+ })
50+ }
51+ }
52+
53+ func Test_Os_SetExecutionDir (t * testing.T ) {
54+ tests := map [string ]struct {
55+ executionDirPathAbs string
56+ setExecutionDirPathAbs string
57+ expectedExecutionDirPathAbs string
58+ }{
59+ "Successful set path" : {
60+ executionDirPathAbs : "" ,
61+ setExecutionDirPathAbs : "/path/to/execution/dir" ,
62+ expectedExecutionDirPathAbs : "/path/to/execution/dir" ,
63+ },
64+ "Successfully overwrite path" : {
65+ executionDirPathAbs : "/some/original/path" ,
66+ setExecutionDirPathAbs : "/path/to/execution/dir" ,
67+ expectedExecutionDirPathAbs : "/path/to/execution/dir" ,
68+ },
69+ }
70+ for name , tc := range tests {
71+ t .Run (name , func (t * testing.T ) {
72+ // Setup
73+ os := NewOs ()
74+ os .executionDirPathAbs = tc .executionDirPathAbs
75+
76+ // Run the test
77+ os .SetExecutionDir (tc .setExecutionDirPathAbs )
78+ actualExecutionDirPathAbs := os .executionDirPathAbs
79+
80+ // Assertions
81+ require .Equal (t , tc .expectedExecutionDirPathAbs , actualExecutionDirPathAbs )
82+ })
83+ }
84+ }
85+
2486func Test_Os_Getenv (t * testing.T ) {
2587 tests := map [string ]struct {
2688 key string
@@ -48,17 +110,50 @@ func Test_Os_Getenv(t *testing.T) {
48110 }
49111}
50112
51- func Test_Os_SetenvUnsetenv (t * testing.T ) {
113+ func Test_Os_Getwd (t * testing.T ) {
52114 o := NewOs ()
53- key := "SLACK_TEST_OS_SETENV"
115+ dir , err := o .Getwd ()
116+ require .NoError (t , err )
117+ require .NotEmpty (t , dir )
118+ }
54119
55- err := o .Setenv (key , "test_value" )
120+ func Test_Os_Glob (t * testing.T ) {
121+ o := NewOs ()
122+ // Create a temp file to glob for
123+ tmpDir := t .TempDir ()
124+ f , err := os .CreateTemp (tmpDir , "glob_test_*.txt" )
56125 require .NoError (t , err )
57- require . Equal ( t , "test_value" , o . Getenv ( key ) )
126+ f . Close ( )
58127
59- err = o .Unsetenv ( key )
128+ matches , err : = o .Glob ( tmpDir + "/*.txt" )
60129 require .NoError (t , err )
61- require .Equal (t , "" , o .Getenv (key ))
130+ require .NotEmpty (t , matches )
131+ }
132+
133+ func Test_Os_IsNotExist (t * testing.T ) {
134+ tests := map [string ]struct {
135+ err error
136+ expected bool
137+ }{
138+ "returns true for os.ErrNotExist" : {
139+ err : os .ErrNotExist ,
140+ expected : true ,
141+ },
142+ "returns false for other errors" : {
143+ err : os .ErrPermission ,
144+ expected : false ,
145+ },
146+ "returns false for nil" : {
147+ err : nil ,
148+ expected : false ,
149+ },
150+ }
151+ for name , tc := range tests {
152+ t .Run (name , func (t * testing.T ) {
153+ o := NewOs ()
154+ require .Equal (t , tc .expected , o .IsNotExist (tc .err ))
155+ })
156+ }
62157}
63158
64159func Test_Os_LookupEnv (t * testing.T ) {
@@ -96,57 +191,17 @@ func Test_Os_LookupEnv(t *testing.T) {
96191 }
97192}
98193
99- func Test_Os_Getwd (t * testing.T ) {
100- o := NewOs ()
101- dir , err := o .Getwd ()
102- require .NoError (t , err )
103- require .NotEmpty (t , dir )
104- }
105-
106- func Test_Os_UserHomeDir (t * testing.T ) {
194+ func Test_Os_SetenvUnsetenv (t * testing.T ) {
107195 o := NewOs ()
108- home , err := o .UserHomeDir ()
109- require .NoError (t , err )
110- require .NotEmpty (t , home )
111- }
112-
113- func Test_Os_IsNotExist (t * testing.T ) {
114- tests := map [string ]struct {
115- err error
116- expected bool
117- }{
118- "returns true for os.ErrNotExist" : {
119- err : os .ErrNotExist ,
120- expected : true ,
121- },
122- "returns false for other errors" : {
123- err : os .ErrPermission ,
124- expected : false ,
125- },
126- "returns false for nil" : {
127- err : nil ,
128- expected : false ,
129- },
130- }
131- for name , tc := range tests {
132- t .Run (name , func (t * testing.T ) {
133- o := NewOs ()
134- require .Equal (t , tc .expected , o .IsNotExist (tc .err ))
135- })
136- }
137- }
196+ key := "SLACK_TEST_OS_SETENV"
138197
139- func Test_Os_Glob (t * testing.T ) {
140- o := NewOs ()
141- // Create a temp file to glob for
142- tmpDir := t .TempDir ()
143- f , err := os .CreateTemp (tmpDir , "glob_test_*.txt" )
198+ err := o .Setenv (key , "test_value" )
144199 require .NoError (t , err )
145- f . Close ( )
200+ require . Equal ( t , "test_value" , o . Getenv ( key ) )
146201
147- matches , err : = o .Glob ( tmpDir + "/*.txt" )
202+ err = o .Unsetenv ( key )
148203 require .NoError (t , err )
149- require .NotEmpty (t , matches )
204+ require .Equal (t , "" , o . Getenv ( key ) )
150205}
151206
152207func Test_Os_Stdout (t * testing.T ) {
@@ -155,64 +210,9 @@ func Test_Os_Stdout(t *testing.T) {
155210 require .NotNil (t , stdout )
156211}
157212
158- func Test_Os_GetExecutionDir (t * testing.T ) {
159- tests := map [string ]struct {
160- executionDirPathAbs string
161- expectedExecutionDirPathAbs string
162- }{
163- "When unset should return blank" : {
164- executionDirPathAbs : "" ,
165- expectedExecutionDirPathAbs : "" ,
166- },
167- "When set should return path" : {
168- executionDirPathAbs : "/path/to/execution/dir" ,
169- expectedExecutionDirPathAbs : "/path/to/execution/dir" ,
170- },
171- }
172- for name , tc := range tests {
173- t .Run (name , func (t * testing.T ) {
174- // Setup
175- os := NewOs ()
176- os .executionDirPathAbs = tc .executionDirPathAbs
177-
178- // Run the test
179- actualExecutionDirPathAbs := os .GetExecutionDir ()
180-
181- // Assertions
182- require .Equal (t , tc .expectedExecutionDirPathAbs , actualExecutionDirPathAbs )
183- })
184- }
185- }
186-
187- func Test_Os_SetExecutionDir (t * testing.T ) {
188- tests := map [string ]struct {
189- executionDirPathAbs string
190- setExecutionDirPathAbs string
191- expectedExecutionDirPathAbs string
192- }{
193- "Successful set path" : {
194- executionDirPathAbs : "" ,
195- setExecutionDirPathAbs : "/path/to/execution/dir" ,
196- expectedExecutionDirPathAbs : "/path/to/execution/dir" ,
197- },
198- "Successfully overwrite path" : {
199- executionDirPathAbs : "/some/original/path" ,
200- setExecutionDirPathAbs : "/path/to/execution/dir" ,
201- expectedExecutionDirPathAbs : "/path/to/execution/dir" ,
202- },
203- }
204- for name , tc := range tests {
205- t .Run (name , func (t * testing.T ) {
206- // Setup
207- os := NewOs ()
208- os .executionDirPathAbs = tc .executionDirPathAbs
209-
210- // Run the test
211- os .SetExecutionDir (tc .setExecutionDirPathAbs )
212- actualExecutionDirPathAbs := os .executionDirPathAbs
213-
214- // Assertions
215- require .Equal (t , tc .expectedExecutionDirPathAbs , actualExecutionDirPathAbs )
216- })
217- }
213+ func Test_Os_UserHomeDir (t * testing.T ) {
214+ o := NewOs ()
215+ home , err := o .UserHomeDir ()
216+ require .NoError (t , err )
217+ require .NotEmpty (t , home )
218218}
0 commit comments