blob: 57ff55aea2a16b235396d90ecf53a9b16a847578 [file] [edit]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package integrationtests
import (
"os"
"path/filepath"
"strings"
"testing"
"go.fuchsia.dev/jiri"
"go.fuchsia.dev/jiri/project"
)
func TestPackageCacheToggle(t *testing.T) {
t.Parallel()
remoteDir := t.TempDir()
setupGitRepo(t, remoteDir, map[string]any{
"manifest": project.Manifest{
Packages: []project.Package{
{
Name: "gn/gn/${platform}",
Path: "prebuilt/third_party/gn",
Version: "git_revision:bdb0fd02324b120cacde634a9235405061c8ea06",
},
},
},
})
root := t.TempDir()
jiriRun := jiriInit(t, root, "-enable-lockfile=false")
jiriRun("import", "manifest", remoteDir)
// 1. Update with cache disabled (default)
jiriRun("update")
resolvedPath := "prebuilt/third_party/gn"
destDir := filepath.Join(root, resolvedPath)
// Verify it is a directory, not a symlink
fi, err := os.Lstat(destDir)
if err != nil {
t.Fatal(err)
}
if fi.Mode()&os.ModeSymlink != 0 {
t.Fatalf("Expected directory, got symlink at %s", destDir)
}
// Create a marker file
markerFile := filepath.Join(destDir, "test_marker")
markerContent := "preserved_content_integration"
if err := os.WriteFile(markerFile, []byte(markerContent), 0644); err != nil {
t.Fatal(err)
}
// 2. Opt-in: Enable cache (preserve lockfile=false)
jiriRun("init", "-package-cache=true")
jiriRun("update")
// Verify destDir is now a symlink
fi, err = os.Lstat(destDir)
if err != nil {
t.Fatal(err)
}
if fi.Mode()&os.ModeSymlink == 0 {
t.Fatalf("Expected symlink, got directory at %s", destDir)
}
// Verify symlink points to cache
linkTarget, err := os.Readlink(destDir)
if err != nil {
t.Fatal(err)
}
expectedCachePrefix := filepath.Join(root, jiri.RootMetaDir, "packages")
if !strings.HasPrefix(linkTarget, expectedCachePrefix) {
t.Errorf("Expected symlink to point to cache %s, got %s", expectedCachePrefix, linkTarget)
}
// Verify marker file is still there
markerInDest := filepath.Join(destDir, "test_marker")
gotContent, err := os.ReadFile(markerInDest)
if err != nil {
t.Fatalf("Marker file lost during opt-in: %v", err)
}
if string(gotContent) != markerContent {
t.Errorf("Marker content mismatch: got %q, want %q", string(gotContent), markerContent)
}
// 3. Opt-out: Disable cache (preserve lockfile=false)
jiriRun("init", "-package-cache=false")
jiriRun("update")
// Verify destDir is a directory again
fi, err = os.Lstat(destDir)
if err != nil {
t.Fatal(err)
}
if fi.Mode()&os.ModeSymlink != 0 {
t.Fatalf("Expected directory, got symlink at %s after opt-out", destDir)
}
// Verify marker file is still there
gotContent, err = os.ReadFile(markerFile)
if err != nil {
t.Fatalf("Marker file lost during opt-out: %v", err)
}
if string(gotContent) != markerContent {
t.Errorf("Marker content mismatch after opt-out: got %q, want %q", string(gotContent), markerContent)
}
}