Skip to content

Commit 841bacd

Browse files
committed
test: add tests for ush caching of varying targets
1 parent 6109037 commit 841bacd

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

internal/cache/cache_test.go

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ func TestCache_SharedFiles_Restoration(t *testing.T) {
605605
for _, file := range sharedFiles {
606606
path := filepath.Join(restoreDir, "SPlsWork", file)
607607
assert.FileExists(t, path, "Shared file %s should be restored", file)
608-
608+
609609
content, err := os.ReadFile(path)
610610
require.NoError(t, err)
611611
assert.Equal(t, fmt.Sprintf("content of %s", file), string(content))
@@ -760,3 +760,110 @@ func TestCache_SharedFiles_NotDuplicated(t *testing.T) {
760760
require.NoError(t, err)
761761
assert.Equal(t, info1.ModTime(), info2.ModTime(), "Shared file should not be re-cached")
762762
}
763+
764+
// TestCache_UshFiles_TargetSpecific verifies that .ush files are cached per-target
765+
// and the correct .ush file is restored for each target
766+
func TestCache_UshFiles_TargetSpecific(t *testing.T) {
767+
cacheDir := t.TempDir()
768+
sourceDir := t.TempDir()
769+
sourceFile := filepath.Join(sourceDir, "test.usp")
770+
splsWorkDir := filepath.Join(sourceDir, "SPlsWork")
771+
772+
// Create source file
773+
err := os.WriteFile(sourceFile, []byte("test source"), 0o644)
774+
require.NoError(t, err)
775+
776+
// Create SPlsWork directory
777+
err = os.MkdirAll(splsWorkDir, 0o755)
778+
require.NoError(t, err)
779+
780+
// Create cache
781+
cache, err := New(cacheDir)
782+
require.NoError(t, err)
783+
defer cache.Close()
784+
785+
// Test with different targets and different .ush content
786+
type targetTest struct {
787+
target string
788+
ushContent string
789+
dllFile string
790+
}
791+
792+
tests := []targetTest{
793+
{target: "2", ushContent: "USH content for series 2 with SMWRev=2.02.05", dllFile: "S2_test.elf"},
794+
{target: "3", ushContent: "USH content for series 3 with SMWRev=3.00.00", dllFile: "test.dll"},
795+
{target: "34", ushContent: "USH content for series 34 combined", dllFile: "test.dll"},
796+
}
797+
798+
cachedUshContents := make(map[string]string)
799+
800+
// Cache builds for each target with different .ush files
801+
for _, tt := range tests {
802+
// Create target-specific .ush file
803+
ushFile := filepath.Join(sourceDir, "test.ush")
804+
err := os.WriteFile(ushFile, []byte(tt.ushContent), 0o644)
805+
require.NoError(t, err)
806+
807+
// Create target-specific output file
808+
outputFile := filepath.Join(splsWorkDir, tt.dllFile)
809+
err = os.WriteFile(outputFile, []byte(fmt.Sprintf("output for %s", tt.target)), 0o644)
810+
require.NoError(t, err)
811+
812+
cfg := &config.Config{Target: tt.target, UserFolders: []string{}}
813+
814+
// Store in cache
815+
err = cache.Store(sourceFile, cfg, true)
816+
require.NoError(t, err)
817+
818+
// Remember what we cached
819+
cachedUshContents[tt.target] = tt.ushContent
820+
821+
// Verify it was cached
822+
hash, err := HashSource(sourceFile, cfg)
823+
require.NoError(t, err)
824+
cachedUshPath := filepath.Join(cacheDir, "artifacts", hash, "test.ush")
825+
assert.FileExists(t, cachedUshPath, ".ush should be cached for target %s", tt.target)
826+
827+
content, err := os.ReadFile(cachedUshPath)
828+
require.NoError(t, err)
829+
assert.Equal(t, tt.ushContent, string(content), "Cached .ush content should match for target %s", tt.target)
830+
831+
// Clean up for next iteration
832+
err = os.Remove(ushFile)
833+
require.NoError(t, err)
834+
err = os.Remove(outputFile)
835+
require.NoError(t, err)
836+
}
837+
838+
// Now verify restoration - each target should restore its own .ush file
839+
for _, tt := range tests {
840+
cfg := &config.Config{Target: tt.target, UserFolders: []string{}}
841+
842+
// Get cache entry
843+
entry, err := cache.Get(sourceFile, cfg)
844+
require.NoError(t, err)
845+
require.NotNil(t, entry, "Should have cache entry for target %s", tt.target)
846+
847+
// Restore to a clean directory
848+
restoreDir := t.TempDir()
849+
err = cache.Restore(entry, restoreDir)
850+
require.NoError(t, err)
851+
852+
// Verify the correct .ush file was restored
853+
restoredUshPath := filepath.Join(restoreDir, "test.ush")
854+
assert.FileExists(t, restoredUshPath, ".ush should be restored for target %s", tt.target)
855+
856+
content, err := os.ReadFile(restoredUshPath)
857+
require.NoError(t, err)
858+
assert.Equal(t, cachedUshContents[tt.target], string(content),
859+
"Restored .ush should match cached content for target %s", tt.target)
860+
861+
// Verify it's the target-specific content, not from another target
862+
for otherTarget, otherContent := range cachedUshContents {
863+
if otherTarget != tt.target {
864+
assert.NotEqual(t, otherContent, string(content),
865+
"Target %s .ush should not contain target %s content", tt.target, otherTarget)
866+
}
867+
}
868+
}
869+
}

0 commit comments

Comments
 (0)