go.fuchsia.dev/jiri@v0.0.0-20240502161911-b66513b29486/x_test.go (about)

     1  // Copyright 2015 The Vanadium Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package jiri
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  // TestFindRootEnvSymlink checks that FindRoot interprets the value of the
    14  // -root flag as a path, evaluates any symlinks the path might contain, and
    15  // returns the result.
    16  func TestFindRootEnvSymlink(t *testing.T) {
    17  	t.Parallel()
    18  	// Create a temporary directory.
    19  	tmpDir, err := os.MkdirTemp("", "")
    20  	if err != nil {
    21  		t.Fatalf("TempDir() failed: %v", err)
    22  	}
    23  	defer func() { os.RemoveAll(tmpDir) }()
    24  
    25  	// Make sure tmpDir is not a symlink itself.
    26  	tmpDir, err = filepath.EvalSymlinks(tmpDir)
    27  	if err != nil {
    28  		t.Fatalf("EvalSymlinks(%v) failed: %v", tmpDir, err)
    29  	}
    30  
    31  	// Create a directory and a symlink to it.
    32  	root, perm := filepath.Join(tmpDir, "root"), os.FileMode(0700)
    33  	symRoot := filepath.Join(tmpDir, "sym_root")
    34  	if err := os.MkdirAll(root, perm); err != nil {
    35  		t.Fatalf("%s", err)
    36  	}
    37  	if err := os.Symlink(root, symRoot); err != nil {
    38  		t.Fatalf("%s", err)
    39  	}
    40  
    41  	// Set the -root flag to the symlink created above and check that
    42  	// FindRoot() evaluates the symlink.
    43  	rootFlag = symRoot
    44  	if got, want := FindRoot(), root; got != want {
    45  		t.Fatalf("unexpected output: got %v, want %v", got, want)
    46  	}
    47  }