github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/jiritest/xtest/x.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 xtest provides utilities for testing jiri functionality.
     6  package xtest
     7  
     8  import (
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/btwiuse/jiri"
    16  	"github.com/btwiuse/jiri/cmdline"
    17  	"github.com/btwiuse/jiri/color"
    18  	"github.com/btwiuse/jiri/log"
    19  	"github.com/btwiuse/jiri/tool"
    20  )
    21  
    22  // NewX is similar to jiri.NewX, but is meant for usage in a testing environment.
    23  func NewX(t *testing.T) (*jiri.X, func()) {
    24  	ctx := tool.NewContextFromEnv(cmdline.EnvFromOS())
    25  	color := color.NewColor(color.ColorNever)
    26  	logger := log.NewLogger(log.InfoLevel, color, false, 0, time.Second*100, nil, nil)
    27  	root, err := ioutil.TempDir("", "")
    28  	if err != nil {
    29  		t.Fatalf("TempDir() failed: %v", err)
    30  	}
    31  	if err := os.Mkdir(filepath.Join(root, jiri.RootMetaDir), 0755); err != nil {
    32  		t.Fatalf("TempDir() failed: %v", err)
    33  	}
    34  	cleanup := func() {
    35  		if err := os.RemoveAll(root); err != nil {
    36  			t.Fatalf("RemoveAll(%q) failed: %v", root, err)
    37  		}
    38  	}
    39  	return &jiri.X{Context: ctx, Root: root, Jobs: jiri.DefaultJobs, Color: color, Logger: logger, Attempts: 1, LockfileEnabled: false}, cleanup
    40  }