github.com/jgadling/terraform@v0.3.8-0.20150227214559-abd68c2c87bc/config/module/get_git_test.go (about)

     1  package module
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  var testHasGit bool
    11  
    12  func init() {
    13  	if _, err := exec.LookPath("git"); err == nil {
    14  		testHasGit = true
    15  	}
    16  }
    17  
    18  func TestGitGetter_impl(t *testing.T) {
    19  	var _ Getter = new(GitGetter)
    20  }
    21  
    22  func TestGitGetter(t *testing.T) {
    23  	if !testHasGit {
    24  		t.Log("git not found, skipping")
    25  		t.Skip()
    26  	}
    27  
    28  	g := new(GitGetter)
    29  	dst := tempDir(t)
    30  
    31  	// Git doesn't allow nested ".git" directories so we do some hackiness
    32  	// here to get around that...
    33  	moduleDir := filepath.Join(fixtureDir, "basic-git")
    34  	oldName := filepath.Join(moduleDir, "DOTgit")
    35  	newName := filepath.Join(moduleDir, ".git")
    36  	if err := os.Rename(oldName, newName); err != nil {
    37  		t.Fatalf("err: %s", err)
    38  	}
    39  	defer os.Rename(newName, oldName)
    40  
    41  	// With a dir that doesn't exist
    42  	if err := g.Get(dst, testModuleURL("basic-git")); err != nil {
    43  		t.Fatalf("err: %s", err)
    44  	}
    45  
    46  	// Verify the main file exists
    47  	mainPath := filepath.Join(dst, "main.tf")
    48  	if _, err := os.Stat(mainPath); err != nil {
    49  		t.Fatalf("err: %s", err)
    50  	}
    51  }
    52  
    53  func TestGitGetter_branch(t *testing.T) {
    54  	if !testHasGit {
    55  		t.Log("git not found, skipping")
    56  		t.Skip()
    57  	}
    58  
    59  	g := new(GitGetter)
    60  	dst := tempDir(t)
    61  
    62  	// Git doesn't allow nested ".git" directories so we do some hackiness
    63  	// here to get around that...
    64  	moduleDir := filepath.Join(fixtureDir, "basic-git")
    65  	oldName := filepath.Join(moduleDir, "DOTgit")
    66  	newName := filepath.Join(moduleDir, ".git")
    67  	if err := os.Rename(oldName, newName); err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  	defer os.Rename(newName, oldName)
    71  
    72  	url := testModuleURL("basic-git")
    73  	q := url.Query()
    74  	q.Add("ref", "test-branch")
    75  	url.RawQuery = q.Encode()
    76  
    77  	if err := g.Get(dst, url); err != nil {
    78  		t.Fatalf("err: %s", err)
    79  	}
    80  
    81  	// Verify the main file exists
    82  	mainPath := filepath.Join(dst, "main_branch.tf")
    83  	if _, err := os.Stat(mainPath); err != nil {
    84  		t.Fatalf("err: %s", err)
    85  	}
    86  
    87  	// Get again should work
    88  	if err := g.Get(dst, url); err != nil {
    89  		t.Fatalf("err: %s", err)
    90  	}
    91  
    92  	// Verify the main file exists
    93  	mainPath = filepath.Join(dst, "main_branch.tf")
    94  	if _, err := os.Stat(mainPath); err != nil {
    95  		t.Fatalf("err: %s", err)
    96  	}
    97  }
    98  
    99  func TestGitGetter_tag(t *testing.T) {
   100  	if !testHasGit {
   101  		t.Log("git not found, skipping")
   102  		t.Skip()
   103  	}
   104  
   105  	g := new(GitGetter)
   106  	dst := tempDir(t)
   107  
   108  	// Git doesn't allow nested ".git" directories so we do some hackiness
   109  	// here to get around that...
   110  	moduleDir := filepath.Join(fixtureDir, "basic-git")
   111  	oldName := filepath.Join(moduleDir, "DOTgit")
   112  	newName := filepath.Join(moduleDir, ".git")
   113  	if err := os.Rename(oldName, newName); err != nil {
   114  		t.Fatalf("err: %s", err)
   115  	}
   116  	defer os.Rename(newName, oldName)
   117  
   118  	url := testModuleURL("basic-git")
   119  	q := url.Query()
   120  	q.Add("ref", "v1.0")
   121  	url.RawQuery = q.Encode()
   122  
   123  	if err := g.Get(dst, url); err != nil {
   124  		t.Fatalf("err: %s", err)
   125  	}
   126  
   127  	// Verify the main file exists
   128  	mainPath := filepath.Join(dst, "main_tag1.tf")
   129  	if _, err := os.Stat(mainPath); err != nil {
   130  		t.Fatalf("err: %s", err)
   131  	}
   132  
   133  	// Get again should work
   134  	if err := g.Get(dst, url); err != nil {
   135  		t.Fatalf("err: %s", err)
   136  	}
   137  
   138  	// Verify the main file exists
   139  	mainPath = filepath.Join(dst, "main_tag1.tf")
   140  	if _, err := os.Stat(mainPath); err != nil {
   141  		t.Fatalf("err: %s", err)
   142  	}
   143  }