mirror of https://github.com/hashicorp/packer
pull/6942/head^2
parent
852113ed07
commit
e9b6adefea
@ -1,65 +1,66 @@
|
||||
package configfile
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"testing"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testConfigTmpDir_impl(t *testing.T) string {
|
||||
var dir string
|
||||
var err error
|
||||
|
||||
prefix, _ := ConfigTmpDir()
|
||||
if dir, err := ioutil.TempDir(prefix, ""); err == nil {
|
||||
if dir, err = ioutil.TempDir(prefix, ""); err == nil {
|
||||
defer os.RemoveAll(dir)
|
||||
} else {
|
||||
_ := fmt.Errorf("Error making directory: %s", err)
|
||||
t.Fatalf("Error making directory: %s", err)
|
||||
}
|
||||
|
||||
return dir
|
||||
}
|
||||
|
||||
func TestConfigTmpDir(t *testing.T) {
|
||||
testConfigTmpDir_impl(t)
|
||||
}
|
||||
|
||||
func TestConfigTmpDir_noenv_PackerTmpDir(t *testing.T) {
|
||||
oldenv := os.Getenv(EnvPackerTmpDir)
|
||||
var oldenv, cd, dir1, dir2 string
|
||||
var err error
|
||||
|
||||
oldenv = os.Getenv(EnvPackerTmpDir)
|
||||
defer os.Setenv(EnvPackerTmpDir, oldenv)
|
||||
os.Setenv(EnvPackerTmpDir, "")
|
||||
|
||||
dir1 := testConfigTmpDir_impl(t)
|
||||
dir1 = testConfigTmpDir_impl(t)
|
||||
|
||||
cd, err := ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("bad ConfigDir")
|
||||
if cd, err = ConfigDir(); err != nil {
|
||||
t.Fatalf("Error during ConfigDir()")
|
||||
}
|
||||
td := filepath.Join(cd, "tmp")
|
||||
os.Setenv(EnvPackerTmpDir, td)
|
||||
|
||||
dir2 := testConfigTmpDir_impl(t)
|
||||
os.Setenv(EnvPackerTmpDir, filepath.Join(cd, "tmp"))
|
||||
|
||||
dir2 = testConfigTmpDir_impl(t)
|
||||
|
||||
if filepath.Dir(dir1) != filepath.Dir(dir2) {
|
||||
t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2))
|
||||
t.Fatalf("base directories do not match: '%s' vs '%s'", filepath.Dir(dir1), filepath.Dir(dir2))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigTmpDir_PackerTmpDir(t *testing.T) {
|
||||
oldenv := os.Getenv(EnvPackerTmpDir)
|
||||
var oldenv, abspath, dir1, dir2 string
|
||||
var err error
|
||||
|
||||
oldenv = os.Getenv(EnvPackerTmpDir)
|
||||
defer os.Setenv(EnvPackerTmpDir, oldenv)
|
||||
os.Setenv(EnvPackerTmpDir, ".")
|
||||
|
||||
dir1 := testConfigTmpDir_impl(t)
|
||||
dir1 = testConfigTmpDir_impl(t)
|
||||
|
||||
abspath, err := filepath.Abs(".")
|
||||
if err != nil {
|
||||
t.Fatalf("bad absolute path")
|
||||
if abspath, err = filepath.Abs("."); err != nil {
|
||||
t.Fatalf("Error during filepath.ABS()")
|
||||
}
|
||||
dir2 := filepath.Join(abspath, "tmp")
|
||||
|
||||
dir2 = filepath.Join(abspath, "tmp")
|
||||
|
||||
if filepath.Dir(dir1) != filepath.Dir(dir2) {
|
||||
t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2))
|
||||
t.Fatalf("Base directories do not match: '%s' vs '%s'", filepath.Dir(dir1), filepath.Dir(dir2))
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue