config/module: FileGetter tests

This commit is contained in:
Mitchell Hashimoto 2014-09-13 18:54:12 -07:00
parent bb22090040
commit c2fe35e74e
3 changed files with 90 additions and 14 deletions

View File

@ -1,7 +1,6 @@
package module package module
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -44,17 +43,4 @@ func TestFolderStorage(t *testing.T) {
if _, err := os.Stat(mainPath); err != nil { if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
}
func tempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.RemoveAll(dir); err != nil {
t.Fatalf("err: %s", err)
}
return dir
} }

View File

@ -0,0 +1,75 @@
package module
import (
"net/url"
"os"
"path/filepath"
"testing"
)
func TestFileGetter_impl(t *testing.T) {
var _ Getter = new(FileGetter)
}
func TestFileGetter(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
// With a dir that doesn't exist
if err := g.Get(dst, testModuleURL("basic")); 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)
}
}
func TestFileGetter_sourceFile(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
// With a source URL that is a path to a file
u := testModuleURL("basic")
u.Path += "/main.tf"
if err := g.Get(dst, u); err == nil {
t.Fatal("should error")
}
}
func TestFileGetter_sourceNoExist(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
// With a source URL that doesn't exist
u := testModuleURL("basic")
u.Path += "/main"
if err := g.Get(dst, u); err == nil {
t.Fatal("should error")
}
}
func TestFileGetter_dir(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
if err := os.MkdirAll(dst, 0755); err != nil {
t.Fatalf("err: %s", err)
}
// With a dir that exists that isn't a symlink
if err := g.Get(dst, testModuleURL("basic")); err == nil {
t.Fatal("should error")
}
}
func testModuleURL(n string) *url.URL {
u, err := url.Parse(testModule(n))
if err != nil {
panic(err)
}
return u
}

View File

@ -1,12 +1,27 @@
package module package module
import ( import (
"io/ioutil"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"testing"
) )
const fixtureDir = "./test-fixtures" const fixtureDir = "./test-fixtures"
func tempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.RemoveAll(dir); err != nil {
t.Fatalf("err: %s", err)
}
return dir
}
func testModule(n string) string { func testModule(n string) string {
p := filepath.Join(fixtureDir, n) p := filepath.Join(fixtureDir, n)
p, err := filepath.Abs(p) p, err := filepath.Abs(p)