func TestRecursiveFileList(t *testing.T) { exchangeHome, _ := fileutil.ExchangeHome() files, err := fileutil.RecursiveFileList(exchangeHome) if err != nil { t.Errorf("RecursiveFileList() returned error: %v", err) } // Make a map for quick lookup & check for a handful // of files at different levels. fileMap := make(map[string]string, 0) for _, f := range files { fileMap[f] = f } sampleFiles := []string{ getPath("README.md"), getPath("constants/constants.go"), getPath("models/config.go"), getPath("models/generic_file.go"), getPath("testdata/json_objects/intel_obj.json"), getPath("util/logger/logger.go"), } for _, filePath := range sampleFiles { _, present := fileMap[filePath] if present == false { t.Errorf("File '%s' is missing from recursive file list", filePath) } } }
func TestExchangeHome(t *testing.T) { exchangeHome := os.Getenv("EXCHANGE_HOME") goHome := os.Getenv("GOPATH") defer os.Setenv("EXCHANGE_HOME", exchangeHome) defer os.Setenv("GOPATH", goHome) // Should use EXCHANGE_HOME, if it's set... os.Setenv("EXCHANGE_HOME", "/bagman_home") exchangeHome, err := fileutil.ExchangeHome() if err != nil { t.Error(err) } if exchangeHome != "/bagman_home" { t.Errorf("ExchangeHome returned '%s', expected '%s'", exchangeHome, "/bagman_home") } os.Setenv("EXCHANGE_HOME", "") // Otherwise, should use GOPATH os.Setenv("GOPATH", "/go_home") exchangeHome, err = fileutil.ExchangeHome() if err != nil { t.Error(err) } if exchangeHome != "/go_home/src/github.com/APTrust/exchange" { t.Errorf("ExchangeHome returned '%s', expected '%s'", exchangeHome, "/go_home") } os.Setenv("GOPATH", "") // Without EXCHANGE_HOME and GOPATH, we should get an error _, err = fileutil.ExchangeHome() if err == nil { t.Error("ExchangeHome should have an thrown exception.") } }
func TestRelativeToAbsPath(t *testing.T) { // If path is already absolute, it should come back unchanged relPath, err := fileutil.RelativeToAbsPath("/usr/local/config/test.json") if err != nil { t.Errorf("RelativeToAbsPath() returned unexpected error %v", err) } else if relPath != "/usr/local/config/test.json" { t.Errorf("RelativeToAbsPath() altered a path that was already absolute") } // Otherwise, we should get an absolute path that assumes // relPath is relative to ExchangeHome. exHome, err := fileutil.ExchangeHome() if err == nil { relPath, err := fileutil.RelativeToAbsPath("config/test.json") if err != nil { t.Errorf("RelativeToAbsPath() returned unexpected error %v", err) return } expected := filepath.Join(exHome, "config/test.json") if relPath != expected { t.Errorf("Expected path %s, got %s", expected, relPath) } } }
func getPath(filename string) string { exchangeHome, _ := fileutil.ExchangeHome() return filepath.Join(exchangeHome, filename) }