diff --git a/cmd/cbuild/commands/build/buildcprj_test.go b/cmd/cbuild/commands/build/buildcprj_test.go index d757daf3..1cc0b40b 100644 --- a/cmd/cbuild/commands/build/buildcprj_test.go +++ b/cmd/cbuild/commands/build/buildcprj_test.go @@ -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 */ @@ -8,6 +8,7 @@ package build_test import ( "os" + "path/filepath" "testing" "github.com/Open-CMSIS-Pack/cbuild/v2/cmd/cbuild/commands" @@ -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() @@ -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 diff --git a/cmd/cbuild/commands/list/list_contexts_test.go b/cmd/cbuild/commands/list/list_contexts_test.go index 4fa9c5d9..ef8a01e6 100644 --- a/cmd/cbuild/commands/list/list_contexts_test.go +++ b/cmd/cbuild/commands/list/list_contexts_test.go @@ -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 */ @@ -7,6 +7,7 @@ package list_test import ( + "path/filepath" "testing" "github.com/Open-CMSIS-Pack/cbuild/v2/cmd/cbuild/commands" @@ -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() diff --git a/cmd/cbuild/commands/list/list_test.go b/cmd/cbuild/commands/list/list_test.go index 780684bc..e71f37b6 100644 --- a/cmd/cbuild/commands/list/list_test.go +++ b/cmd/cbuild/commands/list/list_test.go @@ -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 */ @@ -14,6 +14,7 @@ import ( ) const testRoot = "../../../../test" +const testDir = "command" func TestListCommand(t *testing.T) { assert := assert.New(t) diff --git a/cmd/cbuild/commands/list/list_toolchains_test.go b/cmd/cbuild/commands/list/list_toolchains_test.go index 5ff23cde..4953c2bc 100644 --- a/cmd/cbuild/commands/list/list_toolchains_test.go +++ b/cmd/cbuild/commands/list/list_toolchains_test.go @@ -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 */ @@ -7,6 +7,7 @@ package list_test import ( + "path/filepath" "testing" "github.com/Open-CMSIS-Pack/cbuild/v2/cmd/cbuild/commands" @@ -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() diff --git a/cmd/cbuild/commands/root_test.go b/cmd/cbuild/commands/root_test.go index 5498a7de..1cda417e 100644 --- a/cmd/cbuild/commands/root_test.go +++ b/cmd/cbuild/commands/root_test.go @@ -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 */ @@ -8,6 +8,7 @@ package commands_test import ( "os" + "path/filepath" "testing" "github.com/Open-CMSIS-Pack/cbuild/v2/cmd/cbuild/commands" @@ -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() @@ -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 diff --git a/pkg/builder/cbuildidx/builder_test.go b/pkg/builder/cbuildidx/builder_test.go index 03704233..a03cf068 100644 --- a/pkg/builder/cbuildidx/builder_test.go +++ b/pkg/builder/cbuildidx/builder_test.go @@ -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") { @@ -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) }) @@ -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"}, }, @@ -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) }) @@ -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) }) @@ -125,12 +126,12 @@ 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) @@ -138,8 +139,8 @@ func TestClean(t *testing.T) { 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) @@ -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{ @@ -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) }) @@ -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() diff --git a/pkg/builder/cproject/builder_test.go b/pkg/builder/cproject/builder_test.go index 1f369796..1bb820a3 100644 --- a/pkg/builder/cproject/builder_test.go +++ b/pkg/builder/cproject/builder_test.go @@ -21,9 +21,10 @@ import ( ) const testRoot = "../../../test" +const testDir = "cproject" func init() { - inittest.TestInitialization(testRoot) + inittest.TestInitialization(testRoot, testDir) } type RunnerMock struct{} @@ -31,11 +32,11 @@ type RunnerMock struct{} func (r RunnerMock) ExecuteCommand(program string, quiet bool, args ...string) (string, error) { if strings.Contains(program, "cbuildgen") { if args[0] == "packlist" { - packlistFile := testRoot + "/run/IntDir/minimal.cpinstall" + packlistFile := filepath.Join(testRoot, testDir, "IntDir/minimal.cpinstall") file, _ := os.Create(packlistFile) defer file.Close() } else if args[0] == "cmake" { - cmakelistFile := testRoot + "/run/IntDir/CMakeLists.txt" + cmakelistFile := filepath.Join(testRoot, testDir, "IntDir/CMakeLists.txt") file, _ := os.Create(cmakelistFile) defer file.Close() } @@ -59,19 +60,19 @@ func TestCheckCprj(t *testing.T) { } t.Run("test valid cprj", func(t *testing.T) { - b.InputFile = testRoot + "/run/minimal.cprj" + b.InputFile = filepath.Join(testRoot, testDir, "minimal.cprj") err := b.checkCprj() 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.checkCprj() assert.Error(err) }) t.Run("test invalid file", func(t *testing.T) { - b.InputFile = testRoot + "/run/invalid-file.cprj" + b.InputFile = filepath.Join(testRoot, testDir, "invalid-file.cprj") err := b.checkCprj() assert.Error(err) }) @@ -87,27 +88,27 @@ func TestGetDirs(t *testing.T) { } t.Run("test default directories", func(t *testing.T) { - b.InputFile = testRoot + "/run/minimal.cprj" + b.InputFile = filepath.Join(testRoot, testDir, "minimal.cprj") dirs, err := b.getDirs() assert.Nil(err) - intDir, _ := filepath.Abs(testRoot + "/run/IntDir") - outDir, _ := filepath.Abs(testRoot + "/run/OutDir") + intDir, _ := filepath.Abs(filepath.Join(testRoot, testDir, "IntDir")) + outDir, _ := filepath.Abs(filepath.Join(testRoot, testDir, "OutDir")) assert.Equal(intDir, dirs.IntDir) assert.Equal(outDir, dirs.OutDir) }) t.Run("test valid directories in cprj", func(t *testing.T) { - b.InputFile = testRoot + "/run/minimal-dirs.cprj" + b.InputFile = filepath.Join(testRoot, testDir, "minimal-dirs.cprj") dirs, err := b.getDirs() assert.Nil(err) - intDir, _ := filepath.Abs(testRoot + "/run/Intermediate") - outDir, _ := filepath.Abs(testRoot + "/run/Output") + intDir, _ := filepath.Abs(filepath.Join(testRoot, testDir, "Intermediate")) + outDir, _ := filepath.Abs(filepath.Join(testRoot, testDir, "Output")) assert.Equal(intDir, dirs.IntDir) assert.Equal(outDir, dirs.OutDir) }) t.Run("test valid directories as arguments", func(t *testing.T) { - b.InputFile = testRoot + "/run/minimal.cprj" + b.InputFile = filepath.Join(testRoot, testDir, "minimal.cprj") b.Options.IntDir = "cmdOptionsIntDir" b.Options.OutDir = "cmdOptionsOutDir" dirs, err := b.getDirs() @@ -119,7 +120,7 @@ func TestGetDirs(t *testing.T) { }) t.Run("test invalid cprj", func(t *testing.T) { - b.InputFile = testRoot + "/run/invalid-file.cprj" + b.InputFile = filepath.Join(testRoot, testDir, "invalid-file.cprj") _, err := b.getDirs() assert.Error(err) }) @@ -139,12 +140,12 @@ func TestClean(t *testing.T) { t.Run("test clean directories, invalid tool", func(t *testing.T) { vars.CbuildgenBin = 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) @@ -152,8 +153,8 @@ func TestClean(t *testing.T) { t.Run("test clean directories", func(t *testing.T) { vars.CbuildgenBin = testRoot + "/bin/cbuildgen" - dirs.IntDir = testRoot + "/run/IntDir" - dirs.OutDir = testRoot + "/run/OutDir" + dirs.IntDir = filepath.Join(testRoot, testDir, "IntDir") + dirs.OutDir = filepath.Join(testRoot, testDir, "OutDir") _ = os.MkdirAll(dirs.IntDir, 0755) _ = os.MkdirAll(dirs.OutDir, 0755) err := b.clean(dirs, vars) @@ -161,8 +162,8 @@ func TestClean(t *testing.T) { }) t.Run("test clean non-existent directories", func(t *testing.T) { - dirs.IntDir = testRoot + "/run/non-existent-intdir" - dirs.OutDir = testRoot + "/run/non-existent-outdir" + dirs.IntDir = filepath.Join(testRoot, testDir, "non-existent-intdir") + dirs.OutDir = filepath.Join(testRoot, testDir, "non-existent-outdir") err := b.clean(dirs, vars) assert.Nil(err) }) @@ -173,7 +174,7 @@ func TestGetInternalVars(t *testing.T) { b := CprjBuilder{ builder.BuilderParams{ Runner: RunnerMock{}, - InputFile: testRoot + "/run/minimal.cprj", + InputFile: filepath.Join(testRoot, testDir, "minimal.cprj"), }, } t.Run("test get internal vars", func(t *testing.T) { @@ -212,15 +213,15 @@ func TestGetJobs(t *testing.T) { func TestBuild(t *testing.T) { assert := assert.New(t) - configs := inittest.GetTestConfigs(testRoot) + configs := inittest.GetTestConfigs(testRoot, testDir) b := CprjBuilder{ builder.BuilderParams{ Runner: RunnerMock{}, - InputFile: testRoot + "/run/minimal.cprj", + InputFile: filepath.Join(testRoot, testDir, "minimal.cprj"), Options: builder.Options{ - IntDir: testRoot + "/run/IntDir", - OutDir: testRoot + "/run/OutDir", + IntDir: filepath.Join(testRoot, testDir, "IntDir"), + OutDir: filepath.Join(testRoot, testDir, "OutDir"), Packs: true, }, InstallConfigs: utils.Configurations{ @@ -261,13 +262,13 @@ func TestBuild(t *testing.T) { }) t.Run("test build lock file", func(t *testing.T) { - b.Options.LockFile = testRoot + "/run/lockfile.cprj" + b.Options.LockFile = filepath.Join(testRoot, testDir, "lockfile.cprj") err := b.Build() assert.Nil(err) }) 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) }) @@ -298,8 +299,8 @@ func TestBuild(t *testing.T) { }) t.Run("test build makefile generator", func(t *testing.T) { - b.Options.IntDir = testRoot + "/run/IntDir" - b.Options.OutDir = testRoot + "/run/OutDir" + b.Options.IntDir = filepath.Join(testRoot, testDir, "IntDir") + b.Options.OutDir = filepath.Join(testRoot, testDir, "OutDir") b.Options.Debug = true b.Options.Generator = "Unix Makefiles" err := b.Build() @@ -318,10 +319,10 @@ func TestBuildFail(t *testing.T) { b := CprjBuilder{ builder.BuilderParams{ Runner: RunnerMock{}, - InputFile: testRoot + "/run/minimal.cprj", + InputFile: filepath.Join(testRoot, testDir, "minimal.cprj"), Options: builder.Options{ - IntDir: testRoot + "/run/IntDir", - OutDir: testRoot + "/run/OutDir", + IntDir: filepath.Join(testRoot, testDir, "IntDir"), + OutDir: filepath.Join(testRoot, testDir, "OutDir"), }, }, } diff --git a/pkg/builder/csolution/builder_test.go b/pkg/builder/csolution/builder_test.go index 7f141187..0924e33f 100644 --- a/pkg/builder/csolution/builder_test.go +++ b/pkg/builder/csolution/builder_test.go @@ -20,9 +20,13 @@ import ( ) const testRoot = "../../../test" +const testDir = "csolution" + +var configs inittest.TestConfigs func init() { - inittest.TestInitialization(testRoot) + inittest.TestInitialization(testRoot, testDir) + configs = inittest.GetTestConfigs(testRoot, testDir) } type RunnerMock struct{} @@ -55,12 +59,10 @@ func (r RunnerMock) ExecuteCommand(program string, quiet bool, args ...string) ( func TestListContexts(t *testing.T) { assert := assert.New(t) - configs := inittest.GetTestConfigs(testRoot) - b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, - InputFile: testRoot + "/run/TestSolution/test.csolution.yml", + InputFile: filepath.Join(testRoot, testDir, "TestSolution/test.csolution.yml"), InstallConfigs: utils.Configurations{ BinPath: configs.BinPath, BinExtn: configs.BinExtn, @@ -119,11 +121,10 @@ func TestListContexts(t *testing.T) { func TestListToolchians(t *testing.T) { assert := assert.New(t) - configs := inittest.GetTestConfigs(testRoot) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, - InputFile: testRoot + "/run/test.csolution.yml", + InputFile: filepath.Join(testRoot, testDir, "test.csolution.yml"), InstallConfigs: utils.Configurations{ BinPath: configs.BinPath, BinExtn: configs.BinExtn, @@ -188,7 +189,6 @@ func TestListToolchians(t *testing.T) { func TestListEnvironment(t *testing.T) { assert := assert.New(t) - configs := inittest.GetTestConfigs(testRoot) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, @@ -233,15 +233,13 @@ func TestListEnvironment(t *testing.T) { func TestBuild(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_PACK_ROOT", testRoot+"/run/packs") - configs := inittest.GetTestConfigs(testRoot) + os.Setenv("CMSIS_PACK_ROOT", filepath.Join(testRoot, testDir, "packs")) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, - InputFile: testRoot + "/run/Test.csolution.yml", + InputFile: filepath.Join(testRoot, testDir, "Test.csolution.yml"), Options: builder.Options{ - //IntDir: testRoot + "/run/IntDir", - OutDir: testRoot + "/run/OutDir", + OutDir: filepath.Join(testRoot, testDir, "OutDir"), Packs: true, }, InstallConfigs: utils.Configurations{ @@ -266,14 +264,13 @@ func TestBuild(t *testing.T) { func TestRebuild(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_PACK_ROOT", testRoot+"/run/packs") - configs := inittest.GetTestConfigs(testRoot) + os.Setenv("CMSIS_PACK_ROOT", filepath.Join(testRoot, testDir, "packs")) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, - InputFile: testRoot + "/run/Test.csolution.yml", + InputFile: filepath.Join(testRoot, testDir, "Test.csolution.yml"), Options: builder.Options{ - OutDir: testRoot + "/run/OutDir", + OutDir: filepath.Join(testRoot, testDir, "OutDir"), Packs: true, Rebuild: true, }, @@ -293,8 +290,6 @@ func TestRebuild(t *testing.T) { func TestInstallMissingPacks(t *testing.T) { assert := assert.New(t) - configs := inittest.GetTestConfigs(testRoot) - b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, @@ -322,8 +317,7 @@ func TestInstallMissingPacks(t *testing.T) { func TestGetCprjFilePath(t *testing.T) { assert := assert.New(t) - - testIdxFile := testRoot + "/run/Test.cbuild-idx.yml" + testIdxFile := filepath.Join(testRoot, testDir, "Test.cbuild-idx.yml") b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, @@ -351,14 +345,13 @@ func TestGetCprjFilePath(t *testing.T) { testIdxFile, "HelloWorld_cm0plus.Debug+FRDM-K32L3A6") assert.Nil(err) - assert.Equal(path, filepath.Join(testRoot, "run", "cm0plus", "HelloWorld_cm0plus.Debug+FRDM-K32L3A6.cprj")) + assert.Equal(path, filepath.Join(testRoot, testDir, "cm0plus", "HelloWorld_cm0plus.Debug+FRDM-K32L3A6.cprj")) }) } func TestGetSelectedContexts(t *testing.T) { assert := assert.New(t) - - testSetFile := testRoot + "/run/Test.cbuild-set.yml" + testSetFile := filepath.Join(testRoot, testDir, "Test.cbuild-set.yml") b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, @@ -393,7 +386,7 @@ func TestGetSelectedContexts(t *testing.T) { "HelloWorld_cm4.Release+FRDM-K32L3A6", } b.Options.UseContextSet = false - contexts, err := b.getSelectedContexts(testRoot + "/run/Test.cbuild-idx.yml") + contexts, err := b.getSelectedContexts(filepath.Join(testRoot, testDir, "Test.cbuild-idx.yml")) assert.Nil(err) assert.Equal(contexts, expectedContexts) }) @@ -401,7 +394,6 @@ func TestGetSelectedContexts(t *testing.T) { func TestGetIdxFilePath(t *testing.T) { assert := assert.New(t) - b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, @@ -409,7 +401,7 @@ func TestGetIdxFilePath(t *testing.T) { } t.Run("test invalid input file", func(t *testing.T) { - b.InputFile = "run/TestSolution/invalid_file.yml" + b.InputFile = filepath.Join(testRoot, testDir, "TestSolution/invalid_file.yml") path, err := b.getIdxFilePath() assert.Error(err) @@ -417,20 +409,20 @@ func TestGetIdxFilePath(t *testing.T) { }) t.Run("test get idx file path", func(t *testing.T) { - b.InputFile = "run/TestSolution/test.csolution.yml" + b.InputFile = filepath.Join(testRoot, testDir, "TestSolution/test.csolution.yml") path, err := b.getIdxFilePath() assert.Nil(err) - assert.Equal(path, "run/TestSolution/test.cbuild-idx.yml") + assert.Equal(path, utils.NormalizePath(filepath.Join(testRoot, testDir, "TestSolution/test.cbuild-idx.yml"))) }) t.Run("test get idx file path with output path", func(t *testing.T) { - b.InputFile = "run/TestSolution/test.csolution.yml" - b.Options.Output = "run/outdir" + b.InputFile = filepath.Join(testRoot, testDir, "TestSolution/test.csolution.yml") + b.Options.Output = filepath.Join(testRoot, testDir, "outdir") path, err := b.getIdxFilePath() assert.Nil(err) - assert.Equal(path, b.Options.Output+"/test.cbuild-idx.yml") + assert.Equal(path, utils.NormalizePath(b.Options.Output+"/test.cbuild-idx.yml")) }) } @@ -439,27 +431,27 @@ func TestFormulateArg(t *testing.T) { b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, - InputFile: testRoot + "/run/Test.csolution.yml", + InputFile: filepath.Join(testRoot, testDir, "Test.csolution.yml"), }, } t.Run("test default arg", func(t *testing.T) { args, err := b.formulateArgs([]string{"convert"}) - strArg := strings.Join(args, " ") + strArg := utils.NormalizePath(strings.Join(args, " ")) assert.Nil(err) - assert.Equal("convert --solution=../../../test/run/Test.csolution.yml --no-check-schema --no-update-rte", strArg) + assert.Equal("convert --solution=../../../test/"+testDir+"/Test.csolution.yml --no-check-schema --no-update-rte", strArg) }) t.Run("test context-set arg", func(t *testing.T) { b.Options = builder.Options{ - OutDir: testRoot + "/run/OutDir", + OutDir: filepath.Join(testRoot, testDir, "OutDir"), Contexts: []string{"test.Debug+Target", "test.Release+Target"}, UseContextSet: true, } args, err := b.formulateArgs([]string{"convert"}) - strArg := strings.Join(args, " ") + strArg := utils.NormalizePath(strings.Join(args, " ")) assert.Nil(err) - assert.Equal("convert --solution=../../../test/run/Test.csolution.yml --no-check-schema --no-update-rte --context=test.Debug+Target --context=test.Release+Target --context-set", strArg) + assert.Equal("convert --solution=../../../test/"+testDir+"/Test.csolution.yml --no-check-schema --no-update-rte --context=test.Debug+Target --context=test.Release+Target --context-set", strArg) }) } @@ -473,7 +465,7 @@ func TestGetCbuildSetFilePath(t *testing.T) { } t.Run("test invalid input file", func(t *testing.T) { - b.InputFile = "run/TestSolution/invalid_file.yml" + b.InputFile = filepath.Join(testRoot, testDir, "TestSolution/invalid_file.yml") path, err := b.getSetFilePath() assert.Error(err) @@ -481,10 +473,10 @@ func TestGetCbuildSetFilePath(t *testing.T) { }) t.Run("test get cbuild-set file path", func(t *testing.T) { - b.InputFile = "run/TestSolution/test.csolution.yml" + b.InputFile = filepath.Join(testRoot, testDir, "TestSolution/test.csolution.yml") path, err := b.getSetFilePath() assert.Nil(err) - assert.Equal(path, "run/TestSolution/test.cbuild-set.yml") + assert.Equal(path, utils.NormalizePath(filepath.Join(testRoot, testDir, "TestSolution/test.cbuild-set.yml"))) }) } diff --git a/pkg/inittest/inittest.go b/pkg/inittest/inittest.go index 35c7d396..93b70971 100644 --- a/pkg/inittest/inittest.go +++ b/pkg/inittest/inittest.go @@ -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 */ @@ -14,52 +14,55 @@ import ( "os" "path/filepath" "runtime" - "time" cp "github.com/otiai10/copy" ) -func TestInitialization(testRoot string) { +func TestInitialization(testRoot string, testDir string) { + CleanUp(testRoot, testDir) + testDirPath := testRoot + "/" + testDir // Prepare test data - _ = os.RemoveAll(testRoot + "/run") - time.Sleep(2 * time.Second) - _ = cp.Copy(testRoot+"/data", testRoot+"/run") - - _ = os.MkdirAll(testRoot+"/run/bin", 0755) - _ = os.MkdirAll(testRoot+"/run/etc", 0755) - _ = os.MkdirAll(testRoot+"/run/packs", 0755) - _ = os.MkdirAll(testRoot+"/run/IntDir", 0755) - _ = os.MkdirAll(testRoot+"/run/OutDir", 0755) + _ = cp.Copy(testRoot+"/data", testDirPath) + _ = os.MkdirAll(testDirPath+"/bin", 0755) + _ = os.MkdirAll(testDirPath+"/etc", 0755) + _ = os.MkdirAll(testDirPath+"/packs", 0755) + _ = os.MkdirAll(testDirPath+"/IntDir", 0755) + _ = os.MkdirAll(testDirPath+"/OutDir", 0755) var binExtension string if runtime.GOOS == "windows" { binExtension = ".exe" } - cbuildgenBin := testRoot + "/run/bin/cbuildgen" + binExtension + cbuildgenBin := testDirPath + "/bin/cbuildgen" + binExtension file, _ := os.Create(cbuildgenBin) defer file.Close() - csolutionBin := testRoot + "/run/bin/csolution" + binExtension + csolutionBin := testDirPath + "/bin/csolution" + binExtension file, _ = os.Create(csolutionBin) defer file.Close() - cpackgetBin := testRoot + "/run/bin/cpackget" + binExtension + cpackgetBin := testDirPath + "/bin/cpackget" + binExtension file, _ = os.Create(cpackgetBin) defer file.Close() - cbuild2cmakeBin := testRoot + "/run/bin/cbuild2cmake" + binExtension + cbuild2cmakeBin := testDirPath + "/bin/cbuild2cmake" + binExtension file, _ = os.Create(cbuild2cmakeBin) defer file.Close() } +func CleanUp(testRoot string, testDir string) { + testDirPath := testRoot + "/" + testDir + _ = os.RemoveAll(testDirPath) +} + type TestConfigs struct { BinPath string EtcPath string BinExtn string } -func GetTestConfigs(testRoot string) (configs TestConfigs) { +func GetTestConfigs(testRoot string, testDir string) (configs TestConfigs) { if runtime.GOOS == "windows" { configs.BinExtn = ".exe" } - configs.BinPath, _ = filepath.Abs(testRoot + "/run/bin") - configs.EtcPath, _ = filepath.Abs(testRoot + "/run/etc") + configs.BinPath, _ = filepath.Abs(testRoot + "/" + testDir + "/bin") + configs.EtcPath, _ = filepath.Abs(testRoot + "/" + testDir + "/etc") return configs } diff --git a/pkg/utils/configs_test.go b/pkg/utils/configs_test.go index ef6fb341..ed7c6c49 100644 --- a/pkg/utils/configs_test.go +++ b/pkg/utils/configs_test.go @@ -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 */ @@ -9,13 +9,9 @@ package utils import ( "testing" - "github.com/Open-CMSIS-Pack/cbuild/v2/pkg/inittest" "github.com/stretchr/testify/assert" ) -func init() { - inittest.TestInitialization(testRoot) -} func TestGetInstallConfigs(t *testing.T) { assert := assert.New(t) t.Run("test get install configurations", func(t *testing.T) { diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 96928ec7..5a531e1d 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Arm Limited. All rights reserved. + * Copyright (c) 2022-2024 Arm Limited. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,10 +12,16 @@ import ( "regexp" "testing" + "github.com/Open-CMSIS-Pack/cbuild/v2/pkg/inittest" "github.com/stretchr/testify/assert" ) const testRoot = "../../test" +const testDir = "utils" + +func init() { + inittest.TestInitialization(testRoot, testDir) +} func TestGetExecutablePath(t *testing.T) { assert := assert.New(t) @@ -139,7 +145,7 @@ func TestParseCbuildIndexFile(t *testing.T) { }) t.Run("test cbuild-idx file parsing", func(t *testing.T) { - data, err := ParseCbuildIndexFile(testRoot + "/run/Test.cbuild-idx.yml") + data, err := ParseCbuildIndexFile(filepath.Join(testRoot, testDir, "Test.cbuild-idx.yml")) assert.Nil(err) var re = regexp.MustCompile(`^csolution\s[\d]+.[\d+]+.[\d+].*`) assert.True(re.MatchString(data.BuildIdx.GeneratedBy)) @@ -166,7 +172,7 @@ func TestParseCbuildSetFile(t *testing.T) { }) t.Run("test cbuild-set file parsing", func(t *testing.T) { - data, err := ParseCbuildSetFile(testRoot + "/run/Test.cbuild-set.yml") + data, err := ParseCbuildSetFile(filepath.Join(testRoot, testDir, "Test.cbuild-set.yml")) assert.Nil(err) var re = regexp.MustCompile(`^csolution\sversion\s[\d]+.[\d+]+.[\d+].*`) assert.True(re.MatchString(data.ContextSet.GeneratedBy))