@@ -32,43 +32,11 @@ func Test_CreateContext(t *testing.T) {
3232 Replacement : "Select context using `nats context select context-placeholder`" ,
3333 },
3434 ),
35- func (t * testing.T , ctx * core.CheckFuncCtx ) {
36- t .Helper ()
37- result , isSuccessResult := ctx .Result .(* core.SuccessResult )
38- assert .True (
39- t ,
40- isSuccessResult ,
41- "Expected result to be of type *core.SuccessResult, got %s" ,
42- reflect .TypeOf (result ).String (),
43- )
44- assert .NotNil (t , result )
45- expectedContextFile := result .Resource
46- if ! mnq .FileExists (expectedContextFile ) {
47- t .Errorf (
48- "Expected credentials file not found expected [%s] " ,
49- expectedContextFile ,
50- )
51- } else {
52- ctx .Meta ["deleteFiles" ] = []string {expectedContextFile }
53- }
54- },
35+ checkContextFile ,
5536 ),
5637 AfterFunc : core .AfterFuncCombine (
5738 deleteNATSAccount ("NATS" ),
58- func (ctx * core.AfterFuncCtx ) error {
59- if ctx .Meta ["deleteFiles" ] == nil {
60- return nil
61- }
62- filesToDelete := ctx .Meta ["deleteFiles" ].([]string )
63- for _ , file := range filesToDelete {
64- err := os .Remove (file )
65- if err != nil {
66- t .Errorf ("Failed to delete the file : %s" , err )
67- }
68- }
69-
70- return nil
71- },
39+ afterFuncDeleteFiles ,
7240 ),
7341 }))
7442}
@@ -135,6 +103,50 @@ func beforeFuncCopyConfigToTmpHome() core.BeforeFunc {
135103 })
136104}
137105
106+ func checkContextFile (t * testing.T , ctx * core.CheckFuncCtx ) {
107+ t .Helper ()
108+ result , isSuccessResult := ctx .Result .(* core.SuccessResult )
109+ assert .True (t , isSuccessResult , "Expected result to be of type *core.SuccessResult, got %s" , reflect .TypeOf (result ).String ())
110+ assert .NotNil (t , result )
111+ expectedContextFile := result .Resource
112+ if ! mnq .FileExists (expectedContextFile ) {
113+ t .Errorf ("Expected credentials file not found expected [%s]" , expectedContextFile )
114+ } else {
115+ ctx .Meta ["deleteFiles" ] = []string {expectedContextFile }
116+ }
117+ }
118+
119+ func checkContextFileInXDGConfigHome (t * testing.T , ctx * core.CheckFuncCtx ) {
120+ t .Helper ()
121+ checkContextFile (t , ctx )
122+
123+ result := ctx .Result .(* core.SuccessResult )
124+ expectedContextFile := result .Resource
125+ xdgConfigHome := ctx .OverrideEnv ["XDG_CONFIG_HOME" ]
126+ tmpHomeDir := ctx .OverrideEnv ["HOME" ]
127+
128+ expectedContextDir := filepath .Join (xdgConfigHome , "nats" , "context" )
129+ assert .Contains (t , expectedContextFile , expectedContextDir , "Context file should be in XDG_CONFIG_HOME/nats/context, got: %s" , expectedContextFile )
130+
131+ tmpHomeNatsDir := filepath .Join (tmpHomeDir , ".config" , "nats" , "context" )
132+ _ , err := os .Stat (tmpHomeNatsDir )
133+ assert .True (t , os .IsNotExist (err ), "Files should not be created in HOME/.config/nats/context when XDG_CONFIG_HOME is set: %s" , tmpHomeNatsDir )
134+ }
135+
136+ func afterFuncDeleteFiles (ctx * core.AfterFuncCtx ) error {
137+ if ctx .Meta ["deleteFiles" ] == nil {
138+ return nil
139+ }
140+ filesToDelete := ctx .Meta ["deleteFiles" ].([]string )
141+ for _ , file := range filesToDelete {
142+ if err := os .Remove (file ); err != nil {
143+ return err
144+ }
145+ }
146+
147+ return nil
148+ }
149+
138150func Test_CreateContextWithXDGConfigHome (t * testing.T ) {
139151 xdgConfigHomeDir := t .TempDir ()
140152
@@ -163,68 +175,11 @@ func Test_CreateContextWithXDGConfigHome(t *testing.T) {
163175 Replacement : "Select context using `nats context select context-placeholder`" ,
164176 },
165177 ),
166- func (t * testing.T , ctx * core.CheckFuncCtx ) {
167- t .Helper ()
168- result , isSuccessResult := ctx .Result .(* core.SuccessResult )
169- assert .True (
170- t ,
171- isSuccessResult ,
172- "Expected result to be of type *core.SuccessResult, got %s" ,
173- reflect .TypeOf (result ).String (),
174- )
175- assert .NotNil (t , result )
176-
177- xdgConfigHome := ctx .OverrideEnv ["XDG_CONFIG_HOME" ]
178- tmpHomeDir := ctx .OverrideEnv ["HOME" ]
179-
180- expectedContextFile := result .Resource
181- assert .True (
182- t ,
183- mnq .FileExists (expectedContextFile ),
184- "Expected context file not found: %s" ,
185- expectedContextFile ,
186- )
187-
188- expectedContextDir := filepath .Join (xdgConfigHome , "nats" , "context" )
189- assert .Contains (
190- t ,
191- expectedContextFile ,
192- expectedContextDir ,
193- "Context file should be in XDG_CONFIG_HOME/nats/context, got: %s" ,
194- expectedContextFile ,
195- )
196-
197- tmpHomeNatsDir := filepath .Join (tmpHomeDir , ".config" , "nats" , "context" )
198- tmpHomeNatsDirExists := false
199- if _ , err := os .Stat (tmpHomeNatsDir ); err == nil {
200- tmpHomeNatsDirExists = true
201- }
202- assert .False (
203- t ,
204- tmpHomeNatsDirExists ,
205- "Files should not be created in HOME/.config/nats/context when XDG_CONFIG_HOME is set: %s" ,
206- tmpHomeNatsDir ,
207- )
208-
209- ctx .Meta ["deleteFiles" ] = []string {expectedContextFile }
210- },
178+ checkContextFileInXDGConfigHome ,
211179 ),
212180 AfterFunc : core .AfterFuncCombine (
213181 deleteNATSAccount ("NATS" ),
214- func (ctx * core.AfterFuncCtx ) error {
215- if ctx .Meta ["deleteFiles" ] == nil {
216- return nil
217- }
218- filesToDelete := ctx .Meta ["deleteFiles" ].([]string )
219- for _ , file := range filesToDelete {
220- err := os .Remove (file )
221- if err != nil {
222- t .Errorf ("Failed to delete the file : %s" , err )
223- }
224- }
225-
226- return nil
227- },
182+ afterFuncDeleteFiles ,
228183 ),
229184 }))
230185}
0 commit comments