66package config
77
88import (
9+ "fmt"
910 "os"
1011 "path/filepath"
1112 "testing"
@@ -228,6 +229,7 @@ func TestImportAliases(t *testing.T) {
228229 t .Run ("import non-existing file" , func (t * testing.T ) {
229230 err := ImportAliases ("/nonexistent/file.yml" )
230231 require .Error (t , err )
232+ assert .Contains (t , err .Error (), "failed to read import file" )
231233 })
232234
233235 t .Run ("import invalid yaml" , func (t * testing.T ) {
@@ -237,5 +239,164 @@ func TestImportAliases(t *testing.T) {
237239
238240 err := ImportAliases (importFile )
239241 require .Error (t , err )
242+ assert .Contains (t , err .Error (), "failed to parse import file" )
243+ })
244+ }
245+
246+ func TestGetConfigPath (t * testing.T ) {
247+ t .Run ("default config path" , func (t * testing.T ) {
248+ // Reset to default
249+ ConfigPathFunc = getDefaultConfigPath
250+ defer func () { ConfigPathFunc = getDefaultConfigPath }()
251+
252+ path , err := GetConfigPath ()
253+ require .NoError (t , err )
254+ assert .Contains (t , path , ".config/pup/config.yml" )
255+ assert .Contains (t , path , string (filepath .Separator ))
256+ })
257+ }
258+
259+ func TestLoadAliasesErrorPaths (t * testing.T ) {
260+ t .Run ("error getting config path" , func (t * testing.T ) {
261+ originalGetConfigPath := ConfigPathFunc
262+ ConfigPathFunc = func () (string , error ) {
263+ return "" , fmt .Errorf ("mock error" )
264+ }
265+ defer func () { ConfigPathFunc = originalGetConfigPath }()
266+
267+ _ , err := LoadAliases ()
268+ require .Error (t , err )
269+ assert .Contains (t , err .Error (), "mock error" )
270+ })
271+
272+ t .Run ("error reading existing file" , func (t * testing.T ) {
273+ tmpDir := t .TempDir ()
274+ configPath := filepath .Join (tmpDir , "unreadable.yml" )
275+
276+ // Create a directory with the config file name so it can't be read as a file
277+ require .NoError (t , os .Mkdir (configPath , 0755 ))
278+
279+ originalGetConfigPath := ConfigPathFunc
280+ ConfigPathFunc = func () (string , error ) {
281+ return configPath , nil
282+ }
283+ defer func () { ConfigPathFunc = originalGetConfigPath }()
284+
285+ _ , err := LoadAliases ()
286+ require .Error (t , err )
287+ assert .Contains (t , err .Error (), "failed to read config file" )
288+ })
289+ }
290+
291+ func TestSaveAliasesErrorPaths (t * testing.T ) {
292+ t .Run ("error getting config path" , func (t * testing.T ) {
293+ originalGetConfigPath := ConfigPathFunc
294+ ConfigPathFunc = func () (string , error ) {
295+ return "" , fmt .Errorf ("mock error" )
296+ }
297+ defer func () { ConfigPathFunc = originalGetConfigPath }()
298+
299+ err := SaveAliases (map [string ]string {"test" : "value" })
300+ require .Error (t , err )
301+ assert .Contains (t , err .Error (), "mock error" )
302+ })
303+
304+ t .Run ("error creating directory" , func (t * testing.T ) {
305+ // Create a file where the directory should be
306+ tmpDir := t .TempDir ()
307+ blockingFile := filepath .Join (tmpDir , "blocking" )
308+ require .NoError (t , os .WriteFile (blockingFile , []byte ("test" ), 0644 ))
309+
310+ configPath := filepath .Join (blockingFile , "config.yml" )
311+
312+ originalGetConfigPath := ConfigPathFunc
313+ ConfigPathFunc = func () (string , error ) {
314+ return configPath , nil
315+ }
316+ defer func () { ConfigPathFunc = originalGetConfigPath }()
317+
318+ err := SaveAliases (map [string ]string {"test" : "value" })
319+ require .Error (t , err )
320+ assert .Contains (t , err .Error (), "failed to create config directory" )
321+ })
322+
323+ t .Run ("error writing file" , func (t * testing.T ) {
324+ tmpDir := t .TempDir ()
325+ // Create a directory where the file should be
326+ configPath := filepath .Join (tmpDir , "config.yml" )
327+ require .NoError (t , os .Mkdir (configPath , 0755 ))
328+
329+ originalGetConfigPath := ConfigPathFunc
330+ ConfigPathFunc = func () (string , error ) {
331+ return configPath , nil
332+ }
333+ defer func () { ConfigPathFunc = originalGetConfigPath }()
334+
335+ err := SaveAliases (map [string ]string {"test" : "value" })
336+ require .Error (t , err )
337+ assert .Contains (t , err .Error (), "failed to write config file" )
338+ })
339+ }
340+
341+ func TestGetAliasErrorPath (t * testing.T ) {
342+ t .Run ("error loading aliases" , func (t * testing.T ) {
343+ originalGetConfigPath := ConfigPathFunc
344+ ConfigPathFunc = func () (string , error ) {
345+ return "" , fmt .Errorf ("mock error" )
346+ }
347+ defer func () { ConfigPathFunc = originalGetConfigPath }()
348+
349+ _ , err := GetAlias ("test" )
350+ require .Error (t , err )
351+ assert .Contains (t , err .Error (), "mock error" )
352+ })
353+ }
354+
355+ func TestSetAliasErrorPath (t * testing.T ) {
356+ t .Run ("error loading aliases" , func (t * testing.T ) {
357+ originalGetConfigPath := ConfigPathFunc
358+ ConfigPathFunc = func () (string , error ) {
359+ return "" , fmt .Errorf ("mock error" )
360+ }
361+ defer func () { ConfigPathFunc = originalGetConfigPath }()
362+
363+ err := SetAlias ("test" , "value" )
364+ require .Error (t , err )
365+ assert .Contains (t , err .Error (), "mock error" )
366+ })
367+ }
368+
369+ func TestDeleteAliasErrorPath (t * testing.T ) {
370+ t .Run ("error loading aliases" , func (t * testing.T ) {
371+ originalGetConfigPath := ConfigPathFunc
372+ ConfigPathFunc = func () (string , error ) {
373+ return "" , fmt .Errorf ("mock error" )
374+ }
375+ defer func () { ConfigPathFunc = originalGetConfigPath }()
376+
377+ err := DeleteAlias ("test" )
378+ require .Error (t , err )
379+ assert .Contains (t , err .Error (), "mock error" )
380+ })
381+ }
382+
383+ func TestImportAliasesErrorPath (t * testing.T ) {
384+ t .Run ("error loading existing aliases" , func (t * testing.T ) {
385+ tmpDir := t .TempDir ()
386+ importFile := filepath .Join (tmpDir , "import.yml" )
387+ content := `aliases:
388+ test: value
389+ `
390+ require .NoError (t , os .WriteFile (importFile , []byte (content ), 0600 ))
391+
392+ originalGetConfigPath := ConfigPathFunc
393+ ConfigPathFunc = func () (string , error ) {
394+ return "" , fmt .Errorf ("mock error" )
395+ }
396+ defer func () { ConfigPathFunc = originalGetConfigPath }()
397+
398+ err := ImportAliases (importFile )
399+ require .Error (t , err )
400+ assert .Contains (t , err .Error (), "mock error" )
240401 })
241402}
0 commit comments