2026-03-31 18:50:06 +00:00
|
|
|
package build
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"dappco.re/go/core/io"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCache_SetupCache_Good(t *testing.T) {
|
|
|
|
|
fs := io.NewMockMedium()
|
|
|
|
|
cfg := &CacheConfig{
|
|
|
|
|
Enabled: true,
|
|
|
|
|
Paths: []string{
|
|
|
|
|
"cache/go-build",
|
|
|
|
|
"cache/go-mod",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := SetupCache(fs, "/workspace/project", cfg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, cfg)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "/workspace/project/.core/cache", cfg.Directory)
|
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
|
"/workspace/project/cache/go-build",
|
|
|
|
|
"/workspace/project/cache/go-mod",
|
|
|
|
|
}, cfg.Paths)
|
|
|
|
|
|
|
|
|
|
assert.True(t, fs.Exists("/workspace/project/.core/cache"))
|
|
|
|
|
assert.True(t, fs.Exists("/workspace/project/cache/go-build"))
|
|
|
|
|
assert.True(t, fs.Exists("/workspace/project/cache/go-mod"))
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 12:24:03 +00:00
|
|
|
func TestCache_SetupBuildCache_Good(t *testing.T) {
|
|
|
|
|
fs := io.NewMockMedium()
|
|
|
|
|
cfg := &BuildConfig{
|
|
|
|
|
Build: Build{
|
|
|
|
|
Cache: CacheConfig{
|
|
|
|
|
Enabled: true,
|
|
|
|
|
Paths: []string{
|
|
|
|
|
"cache/go-build",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := SetupBuildCache(fs, "/workspace/project", cfg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, cfg)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "/workspace/project/.core/cache", cfg.Build.Cache.Directory)
|
|
|
|
|
assert.Equal(t, []string{"/workspace/project/cache/go-build"}, cfg.Build.Cache.Paths)
|
|
|
|
|
assert.True(t, fs.Exists("/workspace/project/.core/cache"))
|
|
|
|
|
assert.True(t, fs.Exists("/workspace/project/cache/go-build"))
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 18:50:06 +00:00
|
|
|
func TestCache_SetupCache_Good_Disabled(t *testing.T) {
|
|
|
|
|
fs := io.NewMockMedium()
|
|
|
|
|
cfg := &CacheConfig{
|
|
|
|
|
Enabled: false,
|
|
|
|
|
Paths: []string{"cache/go-build"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := SetupCache(fs, "/workspace/project", cfg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Empty(t, fs.Dirs)
|
|
|
|
|
assert.Empty(t, fs.Files)
|
|
|
|
|
assert.Empty(t, cfg.Directory)
|
|
|
|
|
assert.Equal(t, []string{"cache/go-build"}, cfg.Paths)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 12:24:03 +00:00
|
|
|
func TestCache_SetupBuildCache_Good_Disabled(t *testing.T) {
|
|
|
|
|
fs := io.NewMockMedium()
|
|
|
|
|
cfg := &BuildConfig{
|
|
|
|
|
Build: Build{
|
|
|
|
|
Cache: CacheConfig{
|
|
|
|
|
Enabled: false,
|
|
|
|
|
Paths: []string{"cache/go-build"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := SetupBuildCache(fs, "/workspace/project", cfg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Empty(t, fs.Dirs)
|
|
|
|
|
assert.Empty(t, cfg.Build.Cache.Directory)
|
|
|
|
|
assert.Equal(t, []string{"cache/go-build"}, cfg.Build.Cache.Paths)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 18:50:06 +00:00
|
|
|
func TestCache_CacheKey_Good(t *testing.T) {
|
|
|
|
|
first := CacheKey("core-build", Target{OS: "linux", Arch: "amd64"}, &CacheConfig{
|
|
|
|
|
KeyPrefix: "main",
|
|
|
|
|
Paths: []string{
|
|
|
|
|
"cache/go-build",
|
|
|
|
|
"cache/go-mod",
|
|
|
|
|
},
|
|
|
|
|
RestoreKeys: []string{
|
|
|
|
|
"main-linux",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
second := CacheKey("core-build", Target{OS: "linux", Arch: "amd64"}, &CacheConfig{
|
|
|
|
|
KeyPrefix: "main",
|
|
|
|
|
Paths: []string{
|
|
|
|
|
"cache/go-mod",
|
|
|
|
|
"cache/go-build",
|
|
|
|
|
},
|
|
|
|
|
RestoreKeys: []string{
|
|
|
|
|
"main-linux",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
third := CacheKey("core-build", Target{OS: "darwin", Arch: "arm64"}, &CacheConfig{
|
|
|
|
|
KeyPrefix: "main",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, first, second)
|
|
|
|
|
assert.NotEqual(t, first, third)
|
|
|
|
|
assert.Contains(t, first, "main-linux-amd64-")
|
|
|
|
|
}
|
2026-04-01 12:59:08 +00:00
|
|
|
|
|
|
|
|
func TestCache_CacheEnvironment_Good(t *testing.T) {
|
|
|
|
|
t.Run("maps cache directory and Go cache paths to env vars", func(t *testing.T) {
|
|
|
|
|
env := CacheEnvironment(&CacheConfig{
|
|
|
|
|
Enabled: true,
|
|
|
|
|
Paths: []string{
|
|
|
|
|
"/workspace/project/cache/go-build",
|
|
|
|
|
"/workspace/project/cache/go-mod",
|
|
|
|
|
"/workspace/project/cache/go-build",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
|
"GOCACHE=/workspace/project/cache/go-build",
|
|
|
|
|
"GOMODCACHE=/workspace/project/cache/go-mod",
|
|
|
|
|
}, env)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("disabled cache returns no env vars", func(t *testing.T) {
|
|
|
|
|
assert.Nil(t, CacheEnvironment(&CacheConfig{Enabled: false}))
|
|
|
|
|
})
|
|
|
|
|
}
|