Skip to content

Commit

Permalink
[engine] Resolve symlinks in tests
Browse files Browse the repository at this point in the history
Fixes broken tests on Mac (Apple silicon only), where `t.TempDir()`
returns a path under `/var`, which is a symlink to `/private/var`, as
well as in Windows GitHub workflows where there's a similar link.

Change-Id: I91b9723698749a6f231ed44e854d977236d5e9e3
Reviewed-on: https://fuchsia-review.googlesource.com/c/shac-project/shac/+/916072
Reviewed-by: Anthony Fandrianto <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Fuchsia-Auto-Submit: Oliver Newman <[email protected]>
  • Loading branch information
orn688 authored and CQ Bot committed Sep 13, 2023
1 parent 45bc69f commit 868d287
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions internal/engine/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,7 @@ func TestRun_DirOverridden(t *testing.T) {
func TestRun_SpecificFiles(t *testing.T) {
// Not parallelized because it calls os.Chdir.

root := t.TempDir()

// Evaluate symlinks so that tests work on Mac, where t.TempDir() returns
// path under /var, but /var is symlinked to /private/var and os.Getwd()
// returns the path with symlinks resolved.
root, err := filepath.EvalSymlinks(root)
if err != nil {
t.Fatal(err)
}
root := resolvedTempDir(t)

writeFile(t, root, "shac.textproto", prototext.Format(&Document{
Ignore: []string{
Expand Down Expand Up @@ -987,7 +979,7 @@ func TestRun_SCM_Git_Recursive(t *testing.T) {
// b/
// shac.star
// b.txt
root := t.TempDir()
root := resolvedTempDir(t)
initGit(t, root)
for _, p := range []string{"a", "b", "c"} {
if err := os.Mkdir(filepath.Join(root, p), 0o700); err != nil {
Expand Down Expand Up @@ -1093,7 +1085,7 @@ func TestRun_SCM_Git_Recursive_Shared(t *testing.T) {
// internal.star
// d/
// shared2.star
root := t.TempDir()
root := resolvedTempDir(t)
initGit(t, root)
for _, p := range [...]string{"a", "common", filepath.Join("common", "internal"), "d"} {
if err := os.Mkdir(filepath.Join(root, p), 0o700); err != nil {
Expand Down Expand Up @@ -2128,7 +2120,7 @@ func makeGit(t testing.TB) string {
t.Helper()
// scm.go requires two commits. Not really worth fixing yet, it's only
// annoying in unit tests.
root := t.TempDir()
root := resolvedTempDir(t)
initGit(t, root)

writeFile(t, root, "file.txt", "First file\nIt doesn't contain\na lot of lines.\n")
Expand Down Expand Up @@ -2161,6 +2153,21 @@ func copySCM(t testing.TB, dst string) {
}
}

// resolvedTempDir creates a new test tempdir and resolves all symlinks. This is
// useful when creating a temp dir and passing into shac, then comparing some
// output, because shac internally resolves symlinks.
func resolvedTempDir(t testing.TB) string {
d := t.TempDir()
// Evaluate symlinks so that tests work on Mac, where t.TempDir() returns
// path under /var, but /var is symlinked to /private/var and os.Getwd()
// returns the path with symlinks resolved.
d, err := filepath.EvalSymlinks(d)
if err != nil {
t.Fatal(err)
}
return d
}

func copyFile(t testing.TB, dst, src string) {
t.Helper()
d, err := os.ReadFile(src)
Expand Down

0 comments on commit 868d287

Please sign in to comment.