chore: test helpers relocation (#653)

* chore: test helpers relocation

The knative automations run test with all tags set simultaneously.  This
causes collisions when using tags to separate sets.  This commit moves
all helper functions in the function_test package into a file with no
build tags such that any combination of tags will work.

* src: move test helpers into their own package

* src: spelling and language tweaks
This commit is contained in:
Luke Kingland 2021-11-12 22:39:37 +09:00 committed by GitHub
parent 4d29384958
commit e1fa1ad3c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 190 additions and 190 deletions

View File

@ -14,6 +14,7 @@ import (
"knative.dev/kn-plugin-func/buildpacks"
"knative.dev/kn-plugin-func/docker"
"knative.dev/kn-plugin-func/knative"
. "knative.dev/kn-plugin-func/testing"
)
/*
@ -80,7 +81,7 @@ func TestList(t *testing.T) {
// TestNew creates
func TestNew(t *testing.T) {
defer within(t, "testdata/example.com/testnew")()
defer Within(t, "testdata/example.com/testnew")()
verbose := true
client := newClient(verbose)
@ -107,7 +108,7 @@ func TestNew(t *testing.T) {
// TestDeploy updates
func TestDeploy(t *testing.T) {
defer within(t, "testdata/example.com/deploy")()
defer Within(t, "testdata/example.com/deploy")()
verbose := true
client := newClient(verbose)
@ -124,7 +125,7 @@ func TestDeploy(t *testing.T) {
// TestRemove deletes
func TestRemove(t *testing.T) {
defer within(t, "testdata/example.com/remove")()
defer Within(t, "testdata/example.com/remove")()
verbose := true
client := newClient(verbose)
@ -154,7 +155,7 @@ func TestRemove(t *testing.T) {
// templates' copyNode which forces mode 755 for directories.
// See https://github.com/go-git/go-git/issues/364
func TestRemoteRepositories(t *testing.T) {
defer within(t, "testdata/example.com/remote")()
defer Within(t, "testdata/example.com/remote")()
// Write the test template from the remote onto root
client := fn.New(
@ -282,64 +283,3 @@ func waitFor(t *testing.T, c *fn.Client, name string) {
time.Sleep(pollInterval)
}
}
// Create the given directory, CD to it, and return a function which can be
// run in a defer statement to return to the original directory and cleanup.
// Note must be executed, not deferred itself
// NO: defer within(t, "somedir")
// YES: defer within(t, "somedir")()
func within(t *testing.T, root string) func() {
t.Helper()
cwd := pwd(t)
mkdir(t, root)
cd(t, root)
return func() {
cd(t, cwd)
rm(t, root)
}
}
func pwd(t *testing.T) string {
t.Helper()
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
return dir
}
func mkdir(t *testing.T, dir string) {
t.Helper()
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
}
func cd(t *testing.T, dir string) {
t.Helper()
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
}
func rm(t *testing.T, dir string) {
t.Helper()
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
func touch(file string) {
_, err := os.Stat(file)
if os.IsNotExist(err) {
f, err := os.Create(file)
if err != nil {
panic(err)
}
defer f.Close()
}
t := time.Now().Local()
if err := os.Chtimes(file, t, t); err != nil {
panic(err)
}
}

View File

@ -16,6 +16,7 @@ import (
fn "knative.dev/kn-plugin-func"
"knative.dev/kn-plugin-func/mock"
. "knative.dev/kn-plugin-func/testing"
)
const (
@ -35,7 +36,7 @@ const (
// by the client API for those who prefer manual transmissions.
func TestNew(t *testing.T) {
root := "testdata/example.com/testNew"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -48,7 +49,7 @@ func TestNew(t *testing.T) {
// are written on new.
func TestWritesTemplate(t *testing.T) {
root := "testdata/example.com/testWritesTemplate"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -71,7 +72,7 @@ func TestWritesTemplate(t *testing.T) {
// Function does not reinitialize.
func TestExtantAborts(t *testing.T) {
root := "testdata/example.com/testExtantAborts"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -90,7 +91,7 @@ func TestExtantAborts(t *testing.T) {
// (visible) files aborts.
func TestNonemptyAborts(t *testing.T) {
root := "testdata/example.com/testNonemptyAborts"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -114,7 +115,7 @@ func TestNonemptyAborts(t *testing.T) {
func TestHiddenFilesIgnored(t *testing.T) {
// Create a directory for the Function
root := "testdata/example.com/testHiddenFilesIgnored"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -135,7 +136,7 @@ func TestHiddenFilesIgnored(t *testing.T) {
func TestDefaultRuntime(t *testing.T) {
// Create a root for the new Function
root := "testdata/example.com/testDefaultRuntime"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -167,7 +168,7 @@ func TestDefaultRuntime(t *testing.T) {
// $HOME/.config/func/repositories/boson/go/json
func TestRepositoriesExtensible(t *testing.T) {
root := "testdata/example.com/testRepositoriesExtensible"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(
fn.WithRepositories("testdata/repositories"),
@ -189,7 +190,7 @@ func TestRepositoriesExtensible(t *testing.T) {
// TestRuntimeNotFound generates an error (embedded default repository).
func TestRuntimeNotFound(t *testing.T) {
root := "testdata/example.com/testRuntimeNotFound"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -205,7 +206,7 @@ func TestRuntimeNotFound(t *testing.T) {
// when the requested runtime is not found in a given custom repository
func TestRuntimeNotFoundCustom(t *testing.T) {
root := "testdata/example.com/testRuntimeNotFoundCustom"
defer using(t, root)()
defer Using(t, root)()
// Create a new client with path to the extensible templates
client := fn.New(
@ -226,7 +227,7 @@ func TestRuntimeNotFoundCustom(t *testing.T) {
// TestTemplateNotFound generates an error (embedded default repository).
func TestTemplateNotFound(t *testing.T) {
root := "testdata/example.com/testTemplateNotFound"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -243,7 +244,7 @@ func TestTemplateNotFound(t *testing.T) {
// when the requested template is not found in the given custom repository.
func TestTemplateNotFoundCustom(t *testing.T) {
root := "testdata/example.com/testTemplateNotFoundCustom"
defer using(t, root)()
defer Using(t, root)()
// Create a new client with path to extensible templates
client := fn.New(
@ -269,7 +270,7 @@ func TestNamed(t *testing.T) {
// Path which would derive to testWithHame.example.com were it not for the
// explicitly provided name.
root := "testdata/example.com/testNamed"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -300,7 +301,7 @@ func TestNamed(t *testing.T) {
func TestRegistryRequired(t *testing.T) {
// Create a root for the Function
root := "testdata/example.com/testRegistryRequired"
defer using(t, root)()
defer Using(t, root)()
client := fn.New()
var err error
@ -316,7 +317,7 @@ func TestRegistryRequired(t *testing.T) {
func TestDeriveImage(t *testing.T) {
// Create the root Function directory
root := "testdata/example.com/testDeriveImage"
defer using(t, root)()
defer Using(t, root)()
// Create the function which calculates fields such as name and image.
client := fn.New(fn.WithRegistry(TestRegistry))
@ -343,7 +344,7 @@ func TestDeriveImage(t *testing.T) {
func TestDeriveImageDefaultRegistry(t *testing.T) {
// Create the root Function directory
root := "testdata/example.com/testDeriveImageDefaultRegistry"
defer using(t, root)()
defer Using(t, root)()
// Create the function which calculates fields such as name and image.
// Rather than use TestRegistry, use a single-token name and expect
@ -380,7 +381,7 @@ func TestNewDelegates(t *testing.T) {
)
// Create a directory for the test
defer using(t, root)()
defer Using(t, root)()
// Create a client with mocks for each of the subcomponents.
client := fn.New(
@ -448,7 +449,7 @@ func TestNewDelegates(t *testing.T) {
func TestRun(t *testing.T) {
// Create the root Function directory
root := "testdata/example.com/testRun"
defer using(t, root)()
defer Using(t, root)()
// Create a client with the mock runner and the new test Function
runner := mock.NewRunner()
@ -488,7 +489,7 @@ func TestUpdate(t *testing.T) {
)
// Create the root Function directory
defer using(t, root)()
defer Using(t, root)()
// A client with mocks whose implementaton will validate input.
client := fn.New(
@ -560,7 +561,7 @@ func TestRemoveByPath(t *testing.T) {
remover = mock.NewRemover()
)
defer using(t, root)()
defer Using(t, root)()
client := fn.New(
fn.WithRegistry(TestRegistry),
@ -596,7 +597,7 @@ func TestRemoveByName(t *testing.T) {
remover = mock.NewRemover()
)
defer using(t, root)()
defer Using(t, root)()
client := fn.New(
fn.WithRegistry(TestRegistry),
@ -637,7 +638,7 @@ func TestRemoveUninitializedFails(t *testing.T) {
root = "testdata/example.com/testRemoveUninitializedFails"
remover = mock.NewRemover()
)
defer using(t, root)()
defer Using(t, root)()
// remover fails if invoked
remover.RemoveFn = func(name string) error {
@ -694,7 +695,7 @@ func TestListOutsideRoot(t *testing.T) {
// yields an expected, and informative, error.
func TestDeployUnbuilt(t *testing.T) {
root := "testdata/example.com/testDeployUnbuilt" // Root from which to run the test
defer using(t, root)()
defer Using(t, root)()
// New Client
client := fn.New(fn.WithRegistry(TestRegistry))
@ -744,7 +745,7 @@ func TestEmit(t *testing.T) {
// not provided.
func TestWithConfiguredBuilders(t *testing.T) {
root := "testdata/example.com/testConfiguredBuilders" // Root from which to run the test
defer using(t, root)()
defer Using(t, root)()
builders := map[string]string{
"custom": "docker.io/example/custom",
@ -777,7 +778,7 @@ func TestWithConfiguredBuilders(t *testing.T) {
// keyed as "default", this is set as the default Builder.
func TestWithConfiguredBuildersWithDefault(t *testing.T) {
root := "testdata/example.com/testConfiguredBuildersWithDefault" // Root from which to run the test
defer using(t, root)()
defer Using(t, root)()
builders := map[string]string{
"custom": "docker.io/example/custom",
@ -810,7 +811,7 @@ func TestWithConfiguredBuildersWithDefault(t *testing.T) {
// in the Function configuration when it is provided.
func TestWithConfiguredBuildpacks(t *testing.T) {
root := "testdata/example.com/testConfiguredBuildpacks" // Root from which to run the test
defer using(t, root)()
defer Using(t, root)()
buildpacks := []string{
"docker.io/example/custom-buildpack",
@ -882,7 +883,7 @@ func TestRuntimes(t *testing.T) {
// which are successfully initialized using the client library.
func TestCreateStamp(t *testing.T) {
root := "testdata/example.com/testCreateStamp"
defer using(t, root)()
defer Using(t, root)()
start := time.Now()
@ -900,76 +901,3 @@ func TestCreateStamp(t *testing.T) {
t.Fatalf("expected function timestamp to be after '%v', got '%v'", start, f.Created)
}
}
// Helpers ----
// USING: Make specified dir. Return deferrable cleanup fn.
func using(t *testing.T, root string) func() {
t.Helper()
mkdir(t, root)
return func() {
rm(t, root)
}
}
func mkdir(t *testing.T, dir string) {
t.Helper()
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
}
func rm(t *testing.T, dir string) {
t.Helper()
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
// MKTEMP: Create and CD to a temp dir.
// Returns a deferrable cleanup fn.
func mktemp(t *testing.T) (string, func()) {
t.Helper()
tmp := tempdir(t)
owd := pwd(t)
cd(t, tmp)
return tmp, func() {
os.RemoveAll(tmp)
cd(t, owd)
}
}
func tempdir(t *testing.T) string {
d, err := ioutil.TempDir("", "dir")
if err != nil {
t.Fatal(err)
}
return d
}
func pwd(t *testing.T) string {
d, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
return d
}
func cd(t *testing.T, dir string) {
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
}
// TEST REPO URI: Return URI to repo in ./testdata of matching name.
// Suitable as URI for repository override. returns in form file://
// Must be called prior to mktemp in tests which changes current
// working directory as it depends on a relative path.
// Repo uri: file://$(pwd)/testdata/repository.git (unix-like)
// file: //$(pwd)\testdata\repository.git (windows)
func testRepoURI(name string, t *testing.T) string {
t.Helper()
cwd, _ := os.Getwd()
repo := filepath.Join(cwd, "testdata", name+".git")
return fmt.Sprintf(`file://%s`, repo)
}

View File

@ -7,6 +7,7 @@ import (
"testing"
fn "knative.dev/kn-plugin-func"
. "knative.dev/kn-plugin-func/testing"
)
func TestFunction_ImageWithDigest(t *testing.T) {
@ -118,7 +119,7 @@ func Test_DerivedImage(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
root := "testdata/" + tt.fnName
defer using(t, root)()
defer Using(t, root)()
f := fn.Function{
Name: tt.fnName,

View File

@ -10,6 +10,7 @@ import (
"testing"
fn "knative.dev/kn-plugin-func"
. "knative.dev/kn-plugin-func/testing"
)
// RepositoriesTestRepo is the general-purpose example repository for most
@ -21,7 +22,7 @@ const RepositoriesTestRepo = "repository"
// TestRepositoriesList ensures the base case of listing
// repositories without error in the default scenario of builtin only.
func TestRepositoriesList(t *testing.T) {
root, rm := mktemp(t)
root, rm := Mktemp(t)
defer rm()
client := fn.New(fn.WithRepositories(root)) // Explicitly empty
@ -67,8 +68,8 @@ func TestRepositoriesGet(t *testing.T) {
// TestRepositoriesAll ensures repos are returned from
// .All accessor. Tests both builtin and buitlin+extensible cases.
func TestRepositoriesAll(t *testing.T) {
uri := testRepoURI(RepositoriesTestRepo, t)
root, rm := mktemp(t)
uri := TestRepoURI(RepositoriesTestRepo, t)
root, rm := Mktemp(t)
defer rm()
client := fn.New(fn.WithRepositories(root))
@ -104,8 +105,8 @@ func TestRepositoriesAll(t *testing.T) {
// TestRepositoriesAdd checks basic adding of a repository by URI.
func TestRepositoriesAdd(t *testing.T) {
uri := testRepoURI(RepositoriesTestRepo, t) // ./testdata/$RepositoriesTestRepo.git
root, rm := mktemp(t) // create and cd to a temp dir, returning path.
uri := TestRepoURI(RepositoriesTestRepo, t) // ./testdata/$RepositoriesTestRepo.git
root, rm := Mktemp(t) // create and cd to a temp dir, returning path.
defer rm()
// Instantiate the client using the current temp directory as the
@ -142,8 +143,8 @@ func TestRepositoriesAddDeafultName(t *testing.T) {
// repo meant to exemplify the simplest use case: a repo with no metadata
// that simply contains templates, grouped by runtime. It therefore does
// not have a manifest and the default name will therefore be the repo name
uri := testRepoURI(RepositoriesTestRepo, t) // ./testdata/$RepositoriesTestRepo.git
root, rm := mktemp(t)
uri := TestRepoURI(RepositoriesTestRepo, t) // ./testdata/$RepositoriesTestRepo.git
root, rm := Mktemp(t)
defer rm()
client := fn.New(fn.WithRepositories(root))
@ -177,8 +178,8 @@ func TestRepositoriesAddWithManifest(t *testing.T) {
// defines a custom language pack and makes full use of the manifest.yaml.
// The manifest.yaml is included which specifies things like custom templates
// location and (appropos to this test) a default name/
uri := testRepoURI("repository-a", t) // ./testdata/repository-a.git
root, rm := mktemp(t)
uri := TestRepoURI("repository-a", t) // ./testdata/repository-a.git
root, rm := Mktemp(t)
defer rm()
client := fn.New(fn.WithRepositories(root))
@ -208,8 +209,8 @@ func TestRepositoriesAddWithManifest(t *testing.T) {
// TestRepositoriesAddExistingErrors ensures that adding a repository that
// already exists yields an error.
func TestRepositoriesAddExistingErrors(t *testing.T) {
uri := testRepoURI(RepositoriesTestRepo, t)
root, rm := mktemp(t) // create and cd to a temp dir, returning path.
uri := TestRepoURI(RepositoriesTestRepo, t)
root, rm := Mktemp(t) // create and cd to a temp dir, returning path.
defer rm()
// Instantiate the client using the current temp directory as the
@ -242,8 +243,8 @@ func TestRepositoriesAddExistingErrors(t *testing.T) {
// TestRepositoriesRename ensures renaming a repository succeeds.
func TestRepositoriesRename(t *testing.T) {
uri := testRepoURI(RepositoriesTestRepo, t)
root, rm := mktemp(t) // create and cd to a temp dir, returning path.
uri := TestRepoURI(RepositoriesTestRepo, t)
root, rm := Mktemp(t) // create and cd to a temp dir, returning path.
defer rm()
// Instantiate the client using the current temp directory as the
@ -276,8 +277,8 @@ func TestRepositoriesRename(t *testing.T) {
// TestRepositoriesRemove ensures that removing a repository by name
// removes it from the list and FS.
func TestRepositoriesRemove(t *testing.T) {
uri := testRepoURI(RepositoriesTestRepo, t) // ./testdata/repository.git
root, rm := mktemp(t) // create and cd to a temp dir
uri := TestRepoURI(RepositoriesTestRepo, t) // ./testdata/repository.git
root, rm := Mktemp(t) // create and cd to a temp dir
defer rm()
// Instantiate the client using the current temp directory as the
@ -318,8 +319,8 @@ func TestRepositoriesURL(t *testing.T) {
return
}
uri := testRepoURI(RepositoriesTestRepo, t)
root, rm := mktemp(t)
uri := TestRepoURI(RepositoriesTestRepo, t)
root, rm := Mktemp(t)
defer rm()
client := fn.New(fn.WithRepositories(root))

View File

@ -13,6 +13,7 @@ import (
"testing"
fn "knative.dev/kn-plugin-func"
. "knative.dev/kn-plugin-func/testing"
)
// TestTemplatesList ensures that all templates are listed taking into account
@ -101,7 +102,7 @@ func TestTemplatesGet(t *testing.T) {
func TestTemplateEmbedded(t *testing.T) {
// create test directory
root := "testdata/testTemplateEmbedded"
defer using(t, root)()
defer Using(t, root)()
// Client whose internal (builtin default) templates will be used.
client := fn.New(fn.WithRegistry(TestRegistry))
@ -129,7 +130,7 @@ func TestTemplateEmbedded(t *testing.T) {
func TestTemplateCustom(t *testing.T) {
// Create test directory
root := "testdata/testTemplateCustom"
defer using(t, root)()
defer Using(t, root)()
// CLient which uses custom repositories
// in form [provider]/[template], on disk the template is
@ -161,7 +162,7 @@ func TestTemplateCustom(t *testing.T) {
// using this remote by default.
func TestTemplateRemote(t *testing.T) {
root := "testdata/testTemplateRemote"
defer using(t, root)()
defer Using(t, root)()
// The difference between HTTP vs File protocol is internal to the
// go-git library which implements the template writer. As such
@ -204,7 +205,7 @@ func TestTemplateRemote(t *testing.T) {
func TestTemplateDefault(t *testing.T) {
// create test directory
root := "testdata/testTemplateDefault"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -227,7 +228,7 @@ func TestTemplateDefault(t *testing.T) {
func TestTemplateInvalidErrors(t *testing.T) {
// create test directory
root := "testdata/testTemplateInvalidErrors"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -264,7 +265,7 @@ func TestTemplateModeEmbedded(t *testing.T) {
// set up test directory
root := "testdata/testTemplateModeEmbedded"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(fn.WithRegistry(TestRegistry))
@ -298,7 +299,7 @@ func TestTemplateModeCustom(t *testing.T) {
// test directories
root := "testdata/testTemplateModeCustom"
defer using(t, root)()
defer Using(t, root)()
client := fn.New(
fn.WithRegistry(TestRegistry),
@ -333,7 +334,7 @@ func TestTemplateModeRemote(t *testing.T) {
// test directories
root := "testdata/testTemplateModeRemote"
defer using(t, root)()
defer Using(t, root)()
// Clone a repository from a local file path
cwd, err := os.Getwd()

129
testing/testing.go Normal file
View File

@ -0,0 +1,129 @@
// package testing includes minor testing helpers.
//
// These helpers include extensions to the testing nomenclature which exist to
// ease the development of tests for Functions. It is mostly just syntactic
// sugar and closures for creating an removing test directories etc.
// It was originally included in each of the requisite testing packages, but
// since we use both private-access enabled tests (in the function package),
// as well as closed-box tests (in function_test package), and they are gradually
// increasing in size and complexity, the choice was made to choose a small
// dependency over a small amount of copying.
//
// Another reason for including these in a separate locaiton is that they will
// have no tags such that no combination of tags can cause them to either be
// missing or interfere with eachother (a problem encountered with knative
// tooling which by default runs tests with all tags enabled simultaneously)
package testing
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
// USING: Make specified dir. Return deferrable cleanup fn.
// Using the given path, create it as a new directory and return a deferrable
// which will remove it.
// usage:
// defer using(t, "testdata/example.com/someExampleTest")()
func Using(t *testing.T, root string) func() {
t.Helper()
mkdir(t, root)
return func() {
rm(t, root)
}
}
// mkdir creates a directory as a test helper, failing the test on error.
func mkdir(t *testing.T, dir string) {
t.Helper()
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
}
// rm a directory as a test helper, failing the test on error.
func rm(t *testing.T, dir string) {
t.Helper()
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
// Within the given root creates the directory, CDs to it, and rturns a
// closure that when executed (intended in a defer) removes the given dirctory
// and returns the caller to the initial working directory.
// usage:
// defer within(t, "somedir")()
func Within(t *testing.T, root string) func() {
t.Helper()
cwd := pwd(t)
mkdir(t, root)
cd(t, root)
return func() {
cd(t, cwd)
rm(t, root)
}
}
// Mktemp creates a temporary directory, CDs the current processes (test) to
// said directory, and returns the path to said directory.
// Usage:
// path, rm := mktemp(t)
// defer rm()
// CWD is now 'path'
// errors encountererd fail the current test.
func Mktemp(t *testing.T) (string, func()) {
t.Helper()
tmp := tempdir(t)
owd := pwd(t)
cd(t, tmp)
return tmp, func() {
os.RemoveAll(tmp)
cd(t, owd)
}
}
// tempdir creates a new temporary directory and returns its path.
// errors fail the current test.
func tempdir(t *testing.T) string {
d, err := ioutil.TempDir("", "dir")
if err != nil {
t.Fatal(err)
}
return d
}
// pwd prints the current working directory.
// errors fail the test.
func pwd(t *testing.T) string {
t.Helper()
d, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
return d
}
// cd changes directory to the given directory.
// errors fail the given test.
func cd(t *testing.T, dir string) {
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
}
// TEST REPO URI: Return URI to repo in ./testdata of matching name.
// Suitable as URI for repository override. returns in form file://
// Must be called prior to mktemp in tests which changes current
// working directory as it depends on a relative path.
// Repo uri: file://$(pwd)/testdata/repository.git (unix-like)
// file: //$(pwd)\testdata\repository.git (windows)
func TestRepoURI(name string, t *testing.T) string {
t.Helper()
cwd, _ := os.Getwd()
repo := filepath.Join(cwd, "testdata", name+".git")
return fmt.Sprintf(`file://%s`, repo)
}