github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/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  	"v.io/jiri/tool"
    13  )
    14  
    15  // TestFindRootEnvSymlink checks that FindRoot interprets the value of the
    16  // JIRI_ROOT environment variable as a path, evaluates any symlinks the path
    17  // might contain, and returns the result.
    18  func TestFindRootEnvSymlink(t *testing.T) {
    19  	ctx := tool.NewDefaultContext()
    20  
    21  	// Create a temporary directory.
    22  	tmpDir, err := ctx.NewSeq().TempDir("", "")
    23  	if err != nil {
    24  		t.Fatalf("TempDir() failed: %v", err)
    25  	}
    26  	defer func() { ctx.NewSeq().RemoveAll(tmpDir).Done() }()
    27  
    28  	// Make sure tmpDir is not a symlink itself.
    29  	tmpDir, err = filepath.EvalSymlinks(tmpDir)
    30  	if err != nil {
    31  		t.Fatalf("EvalSymlinks(%v) failed: %v", tmpDir, err)
    32  	}
    33  
    34  	// Create a directory and a symlink to it.
    35  	root, perm := filepath.Join(tmpDir, "root"), os.FileMode(0700)
    36  	symRoot := filepath.Join(tmpDir, "sym_root")
    37  	seq := ctx.NewSeq().MkdirAll(root, perm).Symlink(root, symRoot)
    38  	if err := seq.Done(); err != nil {
    39  		t.Fatalf("%v", err)
    40  	}
    41  
    42  	// Set the JIRI_ROOT to the symlink created above and check that FindRoot()
    43  	// evaluates the symlink.
    44  	oldRoot := os.Getenv(RootEnv)
    45  	if err := os.Setenv(RootEnv, symRoot); err != nil {
    46  		t.Fatalf("%v", err)
    47  	}
    48  	defer os.Setenv(RootEnv, oldRoot)
    49  	if got, want := FindRoot(), root; got != want {
    50  		t.Fatalf("unexpected output: got %v, want %v", got, want)
    51  	}
    52  }