Skip to content

Commit

Permalink
Cleanup test env
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 committed Feb 13, 2024
1 parent 65b34ea commit ec73be6
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 135 deletions.
12 changes: 7 additions & 5 deletions cmd/cbuild/commands/build/buildcprj_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
* Copyright (c) 2023-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -8,6 +8,7 @@ package build_test

import (
"os"
"path/filepath"
"testing"

"github.com/Open-CMSIS-Pack/cbuild/v2/cmd/cbuild/commands"
Expand All @@ -16,10 +17,11 @@ import (
)

const testRoot = "../../../../test"
const testDir = "command"

func TestBuildCPRJCommand(t *testing.T) {
assert := assert.New(t)
cprjFile := testRoot + "/run/minimal.cprj"
cprjFile := filepath.Join(testRoot, testDir, "minimal.cprj")

t.Run("multiple arguments", func(t *testing.T) {
cmd := commands.NewRootCmd()
Expand All @@ -38,9 +40,9 @@ func TestBuildCPRJCommand(t *testing.T) {

func TestPreLogConfiguration(t *testing.T) {
assert := assert.New(t)
logDir := testRoot + "/run/log"
logFile := logDir + "/test.log"
cprjFile := testRoot + "/run/minimal.cprj"
logDir := filepath.Join(testRoot, testDir, "log")
logFile := filepath.Join(logDir, "test.log")
cprjFile := filepath.Join(testRoot, testDir, "minimal.cprj")

t.Run("test normal verbosity level", func(t *testing.T) {
// No quiet, No debug
Expand Down
5 changes: 3 additions & 2 deletions cmd/cbuild/commands/list/list_contexts_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
* Copyright (c) 2023-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

package list_test

import (
"path/filepath"
"testing"

"github.com/Open-CMSIS-Pack/cbuild/v2/cmd/cbuild/commands"
Expand All @@ -15,7 +16,7 @@ import (

func TestListContextsCommand(t *testing.T) {
assert := assert.New(t)
csolutionFile := testRoot + "/run/TestSolution/test.csolution.yml"
csolutionFile := filepath.Join(testRoot, testDir, "TestSolution/test.csolution.yml")

t.Run("No arguments", func(t *testing.T) {
cmd := commands.NewRootCmd()
Expand Down
3 changes: 2 additions & 1 deletion cmd/cbuild/commands/list/list_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
* Copyright (c) 2023-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -14,6 +14,7 @@ import (
)

const testRoot = "../../../../test"
const testDir = "command"

func TestListCommand(t *testing.T) {
assert := assert.New(t)
Expand Down
5 changes: 3 additions & 2 deletions cmd/cbuild/commands/list/list_toolchains_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
* Copyright (c) 2023-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

package list_test

import (
"path/filepath"
"testing"

"github.com/Open-CMSIS-Pack/cbuild/v2/cmd/cbuild/commands"
Expand All @@ -15,7 +16,7 @@ import (

func TestListToolchainsCommand(t *testing.T) {
assert := assert.New(t)
csolutionFile := testRoot + "/run/TestSolution/test.csolution.yml"
csolutionFile := filepath.Join(testRoot, testDir, "TestSolution/test.csolution.yml")

t.Run("invalid flag", func(t *testing.T) {
cmd := commands.NewRootCmd()
Expand Down
14 changes: 8 additions & 6 deletions cmd/cbuild/commands/root_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Arm Limited. All rights reserved.
* Copyright (c) 2022-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -8,6 +8,7 @@ package commands_test

import (
"os"
"path/filepath"
"testing"

"github.com/Open-CMSIS-Pack/cbuild/v2/cmd/cbuild/commands"
Expand All @@ -17,15 +18,16 @@ import (
)

const testRoot = "../../../test"
const testDir = "command"

func init() {
inittest.TestInitialization(testRoot)
inittest.TestInitialization(testRoot, testDir)
}

func TestCommands(t *testing.T) {
assert := assert.New(t)
cprjFile := testRoot + "/run/minimal.cprj"
csolutionFile := testRoot + "/run/test.csolution.yml"
cprjFile := filepath.Join(testRoot, testDir, "minimal.cprj")
csolutionFile := filepath.Join(testRoot, testDir, "test.csolution.yml")

t.Run("test version", func(t *testing.T) {
cmd := commands.NewRootCmd()
Expand Down Expand Up @@ -72,8 +74,8 @@ func TestCommands(t *testing.T) {

func TestPreLogConfiguration(t *testing.T) {
assert := assert.New(t)
logDir := testRoot + "/run/log"
logFile := logDir + "/test.log"
logDir := filepath.Join(testRoot, testDir, "log")
logFile := filepath.Join(logDir, "test.log")

t.Run("test normal verbosity level", func(t *testing.T) {
// No quiet, No debug
Expand Down
41 changes: 21 additions & 20 deletions pkg/builder/cbuildidx/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import (
)

const testRoot = "../../../test"
const testDir = "cbuildidx"

func init() {
inittest.TestInitialization(testRoot)
inittest.TestInitialization(testRoot, testDir)
}

type RunnerMock struct{}

func (r RunnerMock) ExecuteCommand(program string, quiet bool, args ...string) (string, error) {
if strings.Contains(program, "cbuild2cmake") {
_ = os.MkdirAll(testRoot+"/run/tmp", 0755)
cmakelistFile := testRoot + "/run/tmp/CMakeLists.txt"
_ = os.MkdirAll(filepath.Join(testRoot, testDir, "tmp"), 0755)
cmakelistFile := filepath.Join(testRoot, testDir, "tmp/CMakeLists.txt")
file, _ := os.Create(cmakelistFile)
defer file.Close()
} else if strings.Contains(program, "cpackget") {
Expand All @@ -53,19 +54,19 @@ func TestCheckCbuildIdx(t *testing.T) {
}

t.Run("test valid cbuild-idx", func(t *testing.T) {
b.InputFile = testRoot + "/run/Hello.cbuild-idx.yml"
b.InputFile = filepath.Join(testRoot, testDir, "Hello.cbuild-idx.yml")
err := b.checkCbuildIdx()
assert.Nil(err)
})

t.Run("test existent file, invalid extension", func(t *testing.T) {
b.InputFile = testRoot + "/run/main.c"
b.InputFile = filepath.Join(testRoot, testDir, "main.c")
err := b.checkCbuildIdx()
assert.Error(err)
})

t.Run("test invalid file", func(t *testing.T) {
b.InputFile = testRoot + "/run/invalid-file.cbuild-idx.yml"
b.InputFile = filepath.Join(testRoot, testDir, "invalid-file.cbuild-idx.yml")
err := b.checkCbuildIdx()
assert.Error(err)
})
Expand All @@ -77,7 +78,7 @@ func TestGetDirs(t *testing.T) {
b := CbuildIdxBuilder{
builder.BuilderParams{
Runner: RunnerMock{},
InputFile: testRoot + "/run/Hello.cbuild-idx.yml",
InputFile: filepath.Join(testRoot, testDir, "Hello.cbuild-idx.yml"),
Options: builder.Options{
Contexts: []string{"Hello.Debug+AVH"},
},
Expand All @@ -87,8 +88,8 @@ func TestGetDirs(t *testing.T) {
t.Run("test valid directories in cprj", func(t *testing.T) {
dirs, err := b.getDirs()
assert.Nil(err)
intDir, _ := filepath.Abs(testRoot + "/run/tmp")
outDir, _ := filepath.Abs(testRoot + "/run/out/AVH")
intDir, _ := filepath.Abs(filepath.Join(testRoot, testDir, "tmp"))
outDir, _ := filepath.Abs(filepath.Join(testRoot, testDir, "out/AVH"))
assert.Equal(intDir, dirs.IntDir)
assert.Equal(outDir, dirs.OutDir)
})
Expand All @@ -98,14 +99,14 @@ func TestGetDirs(t *testing.T) {
b.Options.OutDir = "cmdOptionsOutDir"
dirs, err := b.getDirs()
assert.Nil(err)
intDir, _ := filepath.Abs(testRoot + "/run/tmp")
intDir, _ := filepath.Abs(filepath.Join(testRoot, testDir, "tmp"))
outDir, _ := filepath.Abs(b.Options.OutDir)
assert.Equal(intDir, dirs.IntDir)
assert.Equal(outDir, dirs.OutDir)
})

t.Run("test invalid cprj", func(t *testing.T) {
b.InputFile = testRoot + "/run/invalid-file.cbuild-idx.yml"
b.InputFile = filepath.Join(testRoot, testDir, "invalid-file.cbuild-idx.yml")
_, err := b.getDirs()
assert.Error(err)
})
Expand All @@ -125,21 +126,21 @@ func TestClean(t *testing.T) {
t.Run("test clean directories, invalid tool", func(t *testing.T) {
vars.CmakeBin = testRoot + "/bin/invalid-tool"

dirs.OutDir = testRoot + "/run/OutDir"
dirs.OutDir = filepath.Join(testRoot, testDir, "OutDir")
_ = os.MkdirAll(dirs.OutDir, 0755)
err := b.clean(dirs, vars)
assert.Error(err)

dirs.IntDir = testRoot + "/run/IntDir"
dirs.IntDir = filepath.Join(testRoot, testDir, "IntDir")
_ = os.MkdirAll(dirs.IntDir, 0755)
err = b.clean(dirs, vars)
assert.Error(err)
})

t.Run("test clean directories", func(t *testing.T) {
vars.CmakeBin = testRoot + "/bin/cmake"
dirs.IntDir = testRoot + "/run/tmp"
dirs.OutDir = testRoot + "/run/OutDir"
dirs.IntDir = filepath.Join(testRoot, testDir, "tmp")
dirs.OutDir = filepath.Join(testRoot, testDir, "OutDir")
_ = os.MkdirAll(dirs.IntDir, 0755)
_ = os.MkdirAll(dirs.OutDir, 0755)
err := b.clean(dirs, vars)
Expand All @@ -149,15 +150,15 @@ func TestClean(t *testing.T) {

func TestBuild(t *testing.T) {
assert := assert.New(t)
configs := inittest.GetTestConfigs(testRoot)
configs := inittest.GetTestConfigs(testRoot, testDir)

b := CbuildIdxBuilder{
builder.BuilderParams{
Runner: RunnerMock{},
InputFile: testRoot + "/run/Hello.cbuild-idx.yml",
InputFile: filepath.Join(testRoot, testDir, "Hello.cbuild-idx.yml"),
Options: builder.Options{
Contexts: []string{"Hello.Debug+AVH"},
OutDir: testRoot + "/run/OutDir",
OutDir: filepath.Join(testRoot, testDir, "OutDir"),
Packs: true,
},
InstallConfigs: utils.Configurations{
Expand Down Expand Up @@ -191,7 +192,7 @@ func TestBuild(t *testing.T) {
})

t.Run("test build log", func(t *testing.T) {
b.Options.LogFile = testRoot + "/run/log/test.log"
b.Options.LogFile = filepath.Join(testRoot, testDir, "log/test.log")
err := b.Build()
assert.Nil(err)
})
Expand All @@ -210,7 +211,7 @@ func TestBuild(t *testing.T) {
})

t.Run("test build makefile generator", func(t *testing.T) {
b.Options.OutDir = testRoot + "/run/OutDir"
b.Options.OutDir = filepath.Join(testRoot, testDir, "OutDir")
b.Options.Debug = true
b.Options.Generator = "Unix Makefiles"
err := b.Build()
Expand Down
Loading

0 comments on commit ec73be6

Please sign in to comment.