Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2a87471

Browse files
authored
Only reports may be collected to ARTIFACTS dir (#36)
1 parent 685c106 commit 2a87471

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

pkg/dotenv/loader.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package dotenv
22

33
import (
4+
"os"
5+
46
"github.com/joho/godotenv"
57
)
68

79
// Load the .env file.
810
func Load() error {
9-
return godotenv.Load()
11+
err := godotenv.Load()
12+
if os.IsNotExist(err) {
13+
return nil
14+
}
15+
return err
1016
}

pkg/files/dirs.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,57 @@ package files
33
import (
44
"os"
55
"path"
6+
"strings"
7+
"sync"
68

79
"github.com/magefile/mage/mg"
810
"github.com/wavesoftware/go-ensure"
911
"github.com/wavesoftware/go-magetasks/config"
1012
"github.com/wavesoftware/go-magetasks/pkg/dotenv"
1113
"github.com/wavesoftware/go-magetasks/pkg/output"
14+
"k8s.io/apimachinery/pkg/util/rand"
15+
)
16+
17+
const randomDirLength = 12
18+
19+
var (
20+
randomDir = "mage-build-" + rand.String(randomDirLength)
21+
buildDirOnce sync.Once
1222
)
1323

1424
// EnsureBuildDir creates a build directory.
1525
func EnsureBuildDir() {
16-
mg.Deps(dotenv.Load, output.Setup)
17-
d := path.Join(BuildDir(), "bin")
18-
ensure.NoError(os.MkdirAll(d, os.ModePerm))
26+
buildDirOnce.Do(func() {
27+
mg.Deps(dotenv.Load, output.Setup)
28+
d := path.Join(BuildDir(), "bin")
29+
ensure.NoError(os.MkdirAll(d, os.ModePerm))
30+
ensure.NoError(os.MkdirAll(ReportsDir(), os.ModePerm))
31+
if strings.Contains(ReportsDir(), randomDir) {
32+
output.Println("📁 Reports directory: ", ReportsDir())
33+
}
34+
})
1935
}
2036

2137
// BuildDir returns project build dir.
2238
func BuildDir() string {
39+
buildDir := os.Getenv("MAGE_BUILD_DIR")
40+
if buildDir != "" {
41+
return buildDir
42+
}
43+
buildDir = os.Getenv("BUILD_DIR")
44+
if buildDir != "" {
45+
return buildDir
46+
}
47+
return relativeTo(ProjectDir(), config.Actual().BuildDirPath...)
48+
}
49+
50+
// ReportsDir returns project reports directory.
51+
func ReportsDir() string {
2352
artifacts := os.Getenv("ARTIFACTS")
2453
if artifacts != "" {
25-
return artifacts
54+
return path.Join(artifacts, randomDir)
2655
}
27-
return relativeToProjectRoot(config.Actual().BuildDirPath)
56+
return relativeTo(BuildDir(), "reports")
2857
}
2958

3059
// ProjectDir returns project repo directory.
@@ -37,9 +66,9 @@ func ProjectDir() string {
3766
return repoDir
3867
}
3968

40-
func relativeToProjectRoot(paths []string) string {
69+
func relativeTo(to string, paths ...string) string {
4170
fullpath := make([]string, len(paths)+1)
42-
fullpath[0] = ProjectDir()
71+
fullpath[0] = to
4372
for ix, elem := range paths {
4473
fullpath[ix+1] = elem
4574
}

testing.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ func Test() {
1818
t := tasks.Start("✅", "Testing", true)
1919
args := []string{
2020
"--format", "testname",
21-
"--jsonfile", fmt.Sprintf("%s/tests-output.json", files.BuildDir()),
22-
"--junitfile", fmt.Sprintf("%s/tests-results.junit.xml", files.BuildDir()),
21+
"--jsonfile", fmt.Sprintf("%s/tests-output.json", files.ReportsDir()),
22+
"--junitfile", fmt.Sprintf("%s/tests-results.junit.xml", files.ReportsDir()),
2323
}
2424
if color.NoColor {
2525
args = append(args, "--no-color")
2626
}
2727
args = append(args, "--",
28-
"-covermode=atomic", fmt.Sprintf("-coverprofile=%s/coverage.out", files.BuildDir()),
28+
"-covermode=atomic", fmt.Sprintf("-coverprofile=%s/tests-coverage.out", files.ReportsDir()),
2929
"-race",
3030
"-count=1",
3131
"-short",

0 commit comments

Comments
 (0)