github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/cmd/jiri/bootstrap_jiri_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 main
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"regexp"
    13  	"strings"
    14  	"testing"
    15  
    16  	"v.io/x/lib/gosh"
    17  )
    18  
    19  func TestBootstrapJiri(t *testing.T) {
    20  	sh := gosh.NewShell(t)
    21  	sh.PropagateChildOutput = true
    22  	defer sh.Cleanup()
    23  
    24  	bootstrap, err := filepath.Abs("../../scripts/bootstrap_jiri")
    25  	if err != nil {
    26  		t.Fatalf("couldn't determine absolute path to bootstrap_jiri script")
    27  	}
    28  	rootDir := filepath.Join(sh.MakeTempDir(), "root")
    29  	stdout, stderr := sh.Cmd(bootstrap, rootDir).StdoutStderr()
    30  	if got, want := stdout, fmt.Sprintf("Please add %s to your PATH.\n", filepath.Join(rootDir, ".jiri_root", "scripts")); got != want {
    31  		t.Errorf("stdout got %q, want %q", got, want)
    32  	}
    33  	if got, want := stderr, ""; got != want {
    34  		t.Errorf("stderr got %q, want %q", got, want)
    35  	}
    36  	if _, err := os.Stat(filepath.Join(rootDir, ".jiri_root", "bin", "jiri")); err != nil {
    37  		t.Error(err)
    38  	}
    39  	if _, err := os.Stat(filepath.Join(rootDir, ".jiri_root", "scripts", "jiri")); err != nil {
    40  		t.Error(err)
    41  	}
    42  }
    43  
    44  // TestBuildJiriLocally checks that the jiri binary built in the bootstrap
    45  // script can be built locally.
    46  func TestBuildJiriLocally(t *testing.T) {
    47  	sh := gosh.NewShell(t)
    48  	sh.PropagateChildOutput = true
    49  	defer sh.Cleanup()
    50  
    51  	// Extract jiri package path from this line.
    52  	// GOPATH="${tmp_dir}" go build -o "${bin_dir}/jiri" v.io/jiri/cmd/jiri
    53  	bootstrap, err := filepath.Abs("../../scripts/bootstrap_jiri")
    54  	if err != nil {
    55  		t.Fatalf("couldn't determine absolute path to bootstrap_jiri script")
    56  	}
    57  	pkgRE := regexp.MustCompile(`.*go build.*\s([^\s]*)\n`)
    58  	content, err := ioutil.ReadFile(bootstrap)
    59  	if err != nil {
    60  		t.Fatalf("couldn't read bootstrap script: %v", err)
    61  	}
    62  	matches := pkgRE.FindStringSubmatch(string(content))
    63  	if len(matches) <= 1 {
    64  		t.Fatalf("couldn't find jiri package from the bootstrap_jiri script")
    65  	}
    66  	pkg := matches[1]
    67  	sh.Cmd("jiri", "go", "build", "-o", filepath.Join(sh.MakeTempDir(), "jiri"), pkg).Run()
    68  }
    69  
    70  func TestBootstrapJiriAlreadyExists(t *testing.T) {
    71  	sh := gosh.NewShell(t)
    72  	sh.PropagateChildOutput = true
    73  	defer sh.Cleanup()
    74  
    75  	bootstrap, err := filepath.Abs("../../scripts/bootstrap_jiri")
    76  	if err != nil {
    77  		t.Fatalf("couldn't determine absolute path to bootstrap_jiri script")
    78  	}
    79  	rootDir := sh.MakeTempDir()
    80  	c := sh.Cmd(bootstrap, rootDir)
    81  	c.ExitErrorIsOk = true
    82  	stdout, stderr := c.StdoutStderr()
    83  	if c.Err == nil {
    84  		t.Errorf("error got %q, want nil", c.Err)
    85  	}
    86  	if got, want := stdout, ""; got != want {
    87  		t.Errorf("stdout got %q, want %q", got, want)
    88  	}
    89  	if got, want := stderr, rootDir+" already exists"; !strings.Contains(got, want) {
    90  		t.Errorf("stderr got %q, want substr %q", got, want)
    91  	}
    92  }