-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[e2e] move CreateTestSessionOutputDir to common package
- Loading branch information
Showing
4 changed files
with
60 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024-present Datadog, Inc. | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
// CreateTestSessionOutputDir creates and returns a directory for tests to store output files and artifacts. | ||
// A timestamp is included in the path to distinguish between multiple runs, and os.MkdirTemp() is | ||
// used to avoid name collisions between parallel runs. | ||
// | ||
// A new directory is created on each call to this function, it is recommended to save this result | ||
// and use it for all tests in a run. For example see BaseSuite.GetRootOutputDir(). | ||
// | ||
// See CreateTestOutputDir and BaseSuite.CreateTestOutputDir for a function that returns a subdirectory for a specific test. | ||
func CreateTestSessionOutputDir(outputRoot string) (string, error) { | ||
// Append timestamp to distinguish between multiple runs | ||
// Format: YYYY-MM-DD_HH-MM-SS | ||
// Use a custom timestamp format because Windows paths can't contain ':' characters | ||
// and we don't need the timezone information. | ||
timePart := time.Now().Format("2006-01-02_15-04-05") | ||
// create root directory | ||
err := os.MkdirAll(outputRoot, 0755) | ||
if err != nil { | ||
return "", err | ||
} | ||
// Create final output directory | ||
// Use MkdirTemp to avoid name collisions between parallel runs | ||
outputRoot, err = os.MkdirTemp(outputRoot, fmt.Sprintf("%s_*", timePart)) | ||
if err != nil { | ||
return "", err | ||
} | ||
if os.Getenv("CI") == "" { | ||
// Create a symlink to the latest run for user convenience | ||
// TODO: Is there a standard "ci" vs "local" check? | ||
// This code used to be in localProfile.GetOutputDir() | ||
latestLink := filepath.Join(filepath.Dir(outputRoot), "latest") | ||
// Remove the symlink if it already exists | ||
if _, err := os.Lstat(latestLink); err == nil { | ||
err = os.Remove(latestLink) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
err = os.Symlink(outputRoot, latestLink) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return outputRoot, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters