Skip to content

Commit

Permalink
Restructure test directories and improve temp dir error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mmulholla committed Feb 10, 2021
1 parent 2b50898 commit ad9d3bd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ main
.vscode/

# File created running tests
tests/tmp/
tests/**/tmp/

.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"github.com/devfile/library/tests/utils"
"github.com/devfile/library/tests/v2/utils"

schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 36 additions & 8 deletions tests/utils/test_utils.go → tests/v2/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import (
)

const (
tmpDir = "../tmp/"
logErrorOnly = false
logFileName = "test.log"
defaultTempDir = "./tmp/"
logFileName = "test.log"
// logToFileOnly - If set to false the log output will also be output to the console
logToFileOnly = true // If set to false the log output will also be output to the console
)

// tmpDir temporary directory in use
var tmpDir string

var (
testLogger *log.Logger
)
Expand All @@ -38,11 +40,14 @@ var (
// - the temporary directory used by the test to store logs and generated devfiles.
// - the log file
func init() {
tmpDir = defaultTempDir
if _, err := os.Stat(tmpDir); !os.IsNotExist(err) {
os.RemoveAll(tmpDir)
}
os.Mkdir(tmpDir, 0755)

if err := os.Mkdir(tmpDir, 0755); err != nil {
fmt.Printf("Failed to create temp directory, will use current directory : %v ", err)
tmpDir = "./"
}
f, err := os.OpenFile(filepath.Join(tmpDir, logFileName), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("Error creating Log file : %v", err)
Expand All @@ -55,11 +60,30 @@ func init() {
}
testLogger.Println("Test Starting:")
}

}

// createTempDir creates a specified sub directory under the temp directory if it does not exist.
// Returns the name of the created directory.
func createTempDir(subdir string) string {
tempDir := tmpDir + subdir + "/"
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
if err = os.Mkdir(tempDir, 0755); err != nil {
LogErrorMessage(fmt.Sprintf("Failed to create temp directory %s will use %s : %v", tempDir, tmpDir, err))
// if cannot create subdirectory just use thebase tmp directory
tempDir = tmpDir
}
} else {
// if cannot create subdirectory just use thebase tmp directory
LogErrorMessage(fmt.Sprintf("Failed to create temp directory %s will use %s : %v", tempDir, tmpDir, err))
tempDir = tmpDir
}
return tempDir
}

// CreateTempDir creates a specified sub directory under the temp directory if it does not exist.
// Returns the name of the created directory.
func CreateTempDir(subdir string) string {
tempDir := tmpDir + subdir + "/"
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
os.Mkdir(tempDir, 0755)
Expand All @@ -78,9 +102,9 @@ func GetDevFileName() string {
testFile := filepath.Base(fn)
testFileExtension := filepath.Ext(testFile)
subdir := testFile[0 : len(testFile)-len(testFileExtension)]
destDir := createTempDir(subdir)
destDir := CreateTempDir(subdir)
callerName := runtime.FuncForPC(pc).Name()
pos1 := strings.LastIndex(callerName, "/tests/api.") + len("/tests/api.")
pos1 := strings.LastIndex(callerName, "/parserTest.") + len("/parserTest.")
devfileName := destDir + callerName[pos1:len(callerName)] + ".yaml"

LogInfoMessage(fmt.Sprintf("GetDevFileName : %s", devfileName))
Expand All @@ -99,7 +123,11 @@ func AddSuffixToFileName(fileName string, suffix string) string {

// LogMessage logs the specified message and returns the message logged
func LogMessage(message string) string {
testLogger.Println(message)
if testLogger != nil {
testLogger.Println(message)
} else {
fmt.Printf("Logger not available: %s", message)
}
return message
}

Expand Down

0 comments on commit ad9d3bd

Please sign in to comment.