terraform/config/module/get_git_test.go

98 lines
2.1 KiB
Go
Raw Normal View History

2014-09-16 08:52:27 +02:00
package module
import (
"os"
"os/exec"
2014-09-16 08:52:27 +02:00
"path/filepath"
"testing"
)
var testHasGit bool
func init() {
if _, err := exec.LookPath("git"); err == nil {
testHasGit = true
}
}
2014-09-16 08:52:27 +02:00
func TestGitGetter_impl(t *testing.T) {
var _ Getter = new(GitGetter)
}
func TestGitGetter(t *testing.T) {
if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}
2014-09-16 08:52:27 +02:00
g := new(GitGetter)
dst := tempDir(t)
// Git doesn't allow nested ".git" directories so we do some hackiness
// here to get around that...
moduleDir := filepath.Join(fixtureDir, "basic-git")
oldName := filepath.Join(moduleDir, "DOTgit")
newName := filepath.Join(moduleDir, ".git")
if err := os.Rename(oldName, newName); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Rename(newName, oldName)
2014-09-16 08:52:27 +02:00
// With a dir that doesn't exist
if err := g.Get(dst, testModuleURL("basic-git")); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
2014-09-16 18:55:51 +02:00
func TestGitGetter_tag(t *testing.T) {
if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}
g := new(GitGetter)
dst := tempDir(t)
// Git doesn't allow nested ".git" directories so we do some hackiness
// here to get around that...
moduleDir := filepath.Join(fixtureDir, "basic-git")
oldName := filepath.Join(moduleDir, "DOTgit")
newName := filepath.Join(moduleDir, ".git")
if err := os.Rename(oldName, newName); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Rename(newName, oldName)
url := testModuleURL("basic-git")
q := url.Query()
q.Add("tag", "v1.0")
url.RawQuery = q.Encode()
if err := g.Get(dst, url); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath := filepath.Join(dst, "main_tag1.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
// Get again should work
if err := g.Get(dst, url); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath = filepath.Join(dst, "main_tag1.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
2014-09-16 18:55:51 +02:00
}