From 79933615f7988cb731baa0768a45b39008f18b20 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 7 Jul 2017 11:30:47 +0200 Subject: add first test --- pkg/rtree/get_test.go | 42 ++++++++++++++++ pkg/rtree/main.go | 7 ++- pkg/rtree/shared_test.go | 126 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 pkg/rtree/get_test.go create mode 100644 pkg/rtree/shared_test.go (limited to 'pkg/rtree') diff --git a/pkg/rtree/get_test.go b/pkg/rtree/get_test.go new file mode 100644 index 0000000..be7e092 --- /dev/null +++ b/pkg/rtree/get_test.go @@ -0,0 +1,42 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* This program is free software: you can redistribute it and/or modify it under +* the terms of the GNU General Public License as published by the Free Software +* Foundation, either version 3 of the License, or (at your option) any later +* version. +* +* This program is distributed in the hope that it will be useful, but WITHOUT ANY +* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +* A PARTICULAR PURPOSE. See the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see . +* +*******************************************************************************/ + +package rtree + +import ( + "path/filepath" + "testing" +) + +func TestGetRepoFromIndex(t *testing.T) { + idx := Index{ + Repos: []*Repo{ + { + CheckoutPath: "github.com/git/git", + Remotes: []Remote{ + {Name: "origin", URL: "gh:git/git"}, + }, + }, + }, + } + Test{ + Args: []string{"get", "gh:git/git"}, + Index: idx, + ExpectOutput: filepath.Join(RootPath, "/github.com/git/git") + "\n", + }.Run(t, "TestGetRepoFromIndex") +} diff --git a/pkg/rtree/main.go b/pkg/rtree/main.go index f08126e..762f457 100644 --- a/pkg/rtree/main.go +++ b/pkg/rtree/main.go @@ -24,8 +24,11 @@ import ( "github.com/majewsky/gofu/pkg/cli" ) -//Exec executes the rtree applet and does not return. The argument is os.Args -//minus the leading "rtree" or "gofu rtree". +//Exec executes the rtree applet and returns an exit code (0 for success, >0 +//for error). The argument is os.Args minus the leading "rtree" or "gofu +//rtree". All side-effects (reading from stdin, writing to stdout/stderr, +//executing other programs) pass through cli.Interface and can be intercepted +//there for the purpose of testing. func Exec(args []string) int { if !Init() { return 1 diff --git a/pkg/rtree/shared_test.go b/pkg/rtree/shared_test.go new file mode 100644 index 0000000..209acb4 --- /dev/null +++ b/pkg/rtree/shared_test.go @@ -0,0 +1,126 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* This program is free software: you can redistribute it and/or modify it under +* the terms of the GNU General Public License as published by the Free Software +* Foundation, either version 3 of the License, or (at your option) any later +* version. +* +* This program is distributed in the hope that it will be useful, but WITHOUT ANY +* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +* A PARTICULAR PURPOSE. See the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see . +* +*******************************************************************************/ + +package rtree + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/majewsky/gofu/pkg/cli" + yaml "gopkg.in/yaml.v2" +) + +//Path to a directory where tests can put their index files. +var indexTmpDir = filepath.Join(os.TempDir(), fmt.Sprintf("rtree-test-%d", os.Getpid())) + +func TestMain(m *testing.M) { + //make sure that test does not accidentally access user's actual rtree or index + os.Setenv("HOME", "") + os.Setenv("GOPATH", "") + //setup test configuration + RootPath = "/unittest/gopath/src" + RemoteAliases = []*RemoteAlias{ + {Alias: "gh:", Replacement: "https://github.com/"}, + {Alias: "my/", Replacement: "git@git.example.com:"}, + } + + exitCode := m.Run() + + //shared teardown + os.RemoveAll(indexTmpDir) + + os.Exit(exitCode) +} + +//Test describes a call to Main(), the environment that's given to it, and the +//assertions that are checked after the call returns. +type Test struct { + Args []string + Input string + Index Index + ExpectFailure bool + ExpectOutput string + ExpectError string + ExpectIndex *Index //if nil, .Index will be used instead +} + +func (test Test) Run(t *testing.T, testName string) { + //write index file, if any + IndexPath = filepath.Join(indexTmpDir, testName+".yaml") + if test.Index.Repos != nil { + err := test.Index.Write() + if err != nil { + t.Fatalf("%s: cannot write index to %s: %s", testName, IndexPath, err.Error()) + } + } + + //setup cli.Interface for test + var stdout bytes.Buffer + var stderr bytes.Buffer + cli.SetupInterface(bytes.NewReader([]byte(test.Input)), &stdout, &stderr, nil) + + //check exit code + exitCode := Exec(test.Args) + switch { + case exitCode == 0 && test.ExpectFailure: + t.Errorf("%s: expected failure, but returned success", testName) + case exitCode != 0 && !test.ExpectFailure: + t.Errorf("%s: expected success, but returned failure", testName) + } + + //check output + output := string(stdout.Bytes()) + if output != test.ExpectOutput { + t.Errorf("%s: expected stdout %#v, but got %#v", testName, test.ExpectOutput, output) + } + output = string(stderr.Bytes()) + if output != test.ExpectError { + t.Errorf("%s: expected stderr %#v, but got %#v", testName, test.ExpectError, output) + } + + //check index + idx := &test.Index + if test.ExpectIndex != nil { + idx = test.ExpectIndex + } + expectedIdxStr, err := yaml.Marshal(idx) + if err != nil { + t.Fatal(err.Error()) + } + actualIdxStr, err := ioutil.ReadFile(IndexPath) + if err != nil { + t.Fatalf("%s: could not read index from %s: %s", testName, IndexPath, err.Error()) + } + if string(expectedIdxStr) != string(actualIdxStr) { + t.Errorf("%s: index does not match expectation after test; diff follows", testName) + cmd := exec.Command("diff", "-u", "-", IndexPath) + cmd.Stdin = bytes.NewReader([]byte(expectedIdxStr)) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Wait() + if err != nil { + t.Fatal(err.Error()) + } + } +} -- cgit v1.3.1