github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/path_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  	"path/filepath"
     9  	"testing"
    10  )
    11  
    12  // TestRelPath checks that RelPath methods return the correct values, and in
    13  // particular that Abs correctly roots the path at the given X's Root.
    14  func TestRelPath(t *testing.T) {
    15  	root1 := "/path/to/jiri-root"
    16  	x := &X{
    17  		Root: root1,
    18  	}
    19  
    20  	rp1 := NewRelPath("foo")
    21  	if got, want := string(rp1), "foo"; got != want {
    22  		t.Errorf("got %v, want %v", got, want)
    23  	}
    24  	if got, want := rp1.Abs(x), filepath.Join(root1, "foo"); got != want {
    25  		t.Errorf("got %v, want %v", got, want)
    26  	}
    27  	if got, want := rp1.Symbolic(), "${"+RootEnv+"}"+string(filepath.Separator)+"foo"; got != want {
    28  		t.Errorf("got %v, want %v", got, want)
    29  	}
    30  
    31  	rp2 := rp1.Join("a", "b")
    32  	if got, want := string(rp2), filepath.Join("foo", "a", "b"); got != want {
    33  		t.Errorf("got %v, want %v", got, want)
    34  	}
    35  	if got, want := rp2.Abs(x), filepath.Join(root1, "foo", "a", "b"); got != want {
    36  		t.Errorf("got %v, want %v", got, want)
    37  	}
    38  	if got, want := rp2.Symbolic(), "${"+RootEnv+"}"+string(filepath.Separator)+filepath.Join("foo", "a", "b"); got != want {
    39  		t.Errorf("got %v, want %v", got, want)
    40  	}
    41  
    42  	// Check Abs with different x.Root.
    43  	root2 := "/different/path"
    44  	x.Root = root2
    45  	if got, want := rp1.Abs(x), filepath.Join(root2, "foo"); got != want {
    46  		t.Errorf("got %v, want %v", got, want)
    47  	}
    48  	if got, want := rp2.Abs(x), filepath.Join(root2, "foo", "a", "b"); got != want {
    49  		t.Errorf("got %v, want %v", got, want)
    50  	}
    51  }