Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure test directories and improve temp dir error handling #59

Merged
merged 2 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
40 changes: 28 additions & 12 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,14 +60,21 @@ func init() {
}
testLogger.Println("Test Starting:")
}

}

// createTempDir creates a specified sub directory under the temp directory if it does not exist.
// 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 {
func CreateTempDir(subdir string) string {
tempDir := tmpDir + subdir + "/"
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
os.Mkdir(tempDir, 0755)
var err error
if _, err = os.Stat(tempDir); os.IsNotExist(err) {
err = os.Mkdir(tempDir, 0755)
}
if err != nil {
// if cannot create subdirectory just use the base tmp directory
LogErrorMessage(fmt.Sprintf("Failed to create temp directory %s will use %s : %v", tempDir, tmpDir, err))
tempDir = tmpDir
}
return tempDir
}
Expand All @@ -78,9 +90,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 +111,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