@@ -8,13 +8,56 @@ import (
88 "testing"
99 "time"
1010
11+ "github.com/microsoft/TypeScript/tsc/internal/core"
1112 "github.com/microsoft/TypeScript/tsc/internal/nativepath"
1213 "github.com/microsoft/TypeScript/tsc/internal/repo"
1314 "github.com/microsoft/TypeScript/tsc/internal/tspath"
1415 "github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
1516 "gotest.tools/v3/assert"
1617)
1718
19+ func TestGetGlobalTypingsCacheLocation (t * testing.T ) {
20+ t .Run ("user cache" , func (t * testing.T ) {
21+ cacheRoot := t .TempDir ()
22+ t .Setenv ("HOME" , cacheRoot )
23+ t .Setenv ("home" , cacheRoot )
24+ t .Setenv ("LocalAppData" , cacheRoot )
25+ t .Setenv ("XDG_CACHE_HOME" , cacheRoot )
26+ cacheDir , err := os .UserCacheDir ()
27+ assert .NilError (t , err )
28+
29+ subdir := "typescript"
30+ if runtime .GOOS == "windows" {
31+ subdir = "Microsoft/TypeScript"
32+ }
33+ assert .Equal (t , osvfs .GetGlobalTypingsCacheLocation (), tspath .RootedDirectoryPathFromAbsolute (tspath .CombinePaths (cacheDir , subdir , core .VersionMajorMinor ())))
34+ })
35+
36+ t .Run ("missing user cache" , func (t * testing.T ) {
37+ for _ , name := range []string {"HOME" , "home" , "LocalAppData" , "XDG_CACHE_HOME" } {
38+ t .Setenv (name , "" )
39+ }
40+ assert .Equal (t , osvfs .GetGlobalTypingsCacheLocation (), tspath .RootedDirectoryPath ("" ))
41+ })
42+
43+ t .Run ("relative XDG cache" , func (t * testing.T ) {
44+ switch runtime .GOOS {
45+ case "windows" , "darwin" , "ios" , "plan9" :
46+ t .Skip ("XDG_CACHE_HOME is not used on this platform" )
47+ }
48+ t .Setenv ("XDG_CACHE_HOME" , "relative" )
49+ assert .Equal (t , osvfs .GetGlobalTypingsCacheLocation (), tspath .RootedDirectoryPath ("" ))
50+ })
51+
52+ t .Run ("relative user cache" , func (t * testing.T ) {
53+ t .Setenv ("XDG_CACHE_HOME" , "" )
54+ for _ , name := range []string {"HOME" , "home" , "LocalAppData" } {
55+ t .Setenv (name , "relative-home" )
56+ }
57+ assert .Equal (t , osvfs .GetGlobalTypingsCacheLocation (), tspath .RootedDirectoryPath ("" ))
58+ })
59+ }
60+
1861func TestOS (t * testing.T ) {
1962 t .Parallel ()
2063
0 commit comments