@@ -16,6 +16,7 @@ package cache
1616import (
1717 "io/ioutil"
1818 "os"
19+ "path/filepath"
1920 "testing"
2021
2122 "github.com/stretchr/testify/assert"
@@ -44,8 +45,77 @@ func TestCacheCreatesDir(t *testing.T) {
4445 }
4546}
4647
48+ func TestCacheDirLinux (t * testing.T ) {
49+ // Mock GetOSName in the test, then reset it after the test
50+ oldGetOSName := getOSName
51+ getOSName = func () string {
52+ return "linux"
53+ }
54+ defer func () { getOSName = oldGetOSName }()
55+
56+ // Create a temporary directory for the dummy ecs config
57+ tempDirName , err := ioutil .TempDir ("" , "test" )
58+ if err != nil {
59+ t .Fatal ("Error while creating the dummy ecs config directory" )
60+ }
61+ defer os .Remove (tempDirName )
62+
63+ os .Setenv ("HOME" , tempDirName )
64+ defer os .Unsetenv ("HOME" )
65+
66+ path , err := cacheDir ("name" )
67+ assert .NoError (t , err , "Unexpected error creating new config path" )
68+ assert .Equal (t , filepath .Join (tempDirName , ".cache" , cachePrefix , "name" ), path )
69+ }
70+
71+ func TestCacheDirDarwin (t * testing.T ) {
72+ // Mock GetOSName in the test, then reset it after the test
73+ oldGetOSName := getOSName
74+ getOSName = func () string {
75+ return "darwin"
76+ }
77+ defer func () { getOSName = oldGetOSName }()
78+
79+ // Create a temporary directory for the dummy ecs config
80+ tempDirName , err := ioutil .TempDir ("" , "test" )
81+ if err != nil {
82+ t .Fatal ("Error while creating the dummy ecs config directory" )
83+ }
84+ defer os .Remove (tempDirName )
85+
86+ os .Setenv ("HOME" , tempDirName )
87+ defer os .Unsetenv ("HOME" )
88+
89+ path , err := cacheDir ("name" )
90+ assert .NoError (t , err , "Unexpected error creating new config path" )
91+ assert .Equal (t , filepath .Join (tempDirName , ".cache" , cachePrefix , "name" ), path )
92+ }
93+
94+ func TestCacheDirWindows (t * testing.T ) {
95+ // Mock GetOSName in the test, then reset it after the test
96+ oldGetOSName := getOSName
97+ getOSName = func () string {
98+ return "windows"
99+ }
100+ defer func () { getOSName = oldGetOSName }()
101+
102+ // Create a temporary directory for the dummy ecs config
103+ tempDirName , err := ioutil .TempDir ("" , "test" )
104+ if err != nil {
105+ t .Fatal ("Error while creating the dummy ecs config directory" )
106+ }
107+ defer os .Remove (tempDirName )
108+
109+ os .Setenv ("HOME" , tempDirName )
110+ defer os .Unsetenv ("HOME" )
111+
112+ path , err := cacheDir ("name" )
113+ assert .NoError (t , err , "Unexpected error creating new config path" )
114+ assert .Equal (t , filepath .Join (tempDirName , "AppData" , "local" , "ecs" , "cache" , "name" ), path )
115+ }
116+
47117func tempDir (t * testing.T ) string {
48- // Create a temprorary directory for the dummy ecs config
118+ // Create a temporary directory for the dummy ecs config
49119 tempDirName , err := ioutil .TempDir ("" , "test" )
50120 assert .NoError (t , err , "Unexpected error while creating the dummy ecs config directory" )
51121 return tempDirName
0 commit comments