diff --git a/cmd/cbuild/commands/build/buildcprj_test.go b/cmd/cbuild/commands/build/buildcprj_test.go index 6b9148b..e2f3251 100644 --- a/cmd/cbuild/commands/build/buildcprj_test.go +++ b/cmd/cbuild/commands/build/buildcprj_test.go @@ -9,34 +9,16 @@ package build_test import ( "cbuild/cmd/cbuild/commands" "os" - "runtime" "testing" - cp "github.com/otiai10/copy" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) const testRoot = "../../../../test" -func init() { - // Prepare test data - _ = os.RemoveAll(testRoot + "/run") - - var binExtension string - if runtime.GOOS == "windows" { - binExtension = ".exe" - } - cbuildgenBin := testRoot + "/run/bin/cbuildgen" + binExtension - file, _ := os.Create(cbuildgenBin) - defer file.Close() - - _ = cp.Copy(testRoot+"/run/minimal.cprj", testRoot+"/run/minimal.cprj") -} - func TestBuildCPRJCommand(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") cprjFile := testRoot + "/run/minimal.cprj" t.Run("multiple arguments", func(t *testing.T) { @@ -56,7 +38,6 @@ func TestBuildCPRJCommand(t *testing.T) { func TestPreLogConfiguration(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") logDir := testRoot + "/run/log" logFile := logDir + "/test.log" cprjFile := testRoot + "/run/minimal.cprj" diff --git a/cmd/cbuild/commands/list/list_contexts_test.go b/cmd/cbuild/commands/list/list_contexts_test.go index 4f400fa..5befa54 100644 --- a/cmd/cbuild/commands/list/list_contexts_test.go +++ b/cmd/cbuild/commands/list/list_contexts_test.go @@ -8,7 +8,6 @@ package list_test import ( "cbuild/cmd/cbuild/commands" - "os" "testing" "github.com/stretchr/testify/assert" @@ -16,7 +15,6 @@ import ( func TestListContextsCommand(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") csolutionFile := testRoot + "/run/TestSolution/test.csolution.yml" t.Run("No arguments", func(t *testing.T) { diff --git a/cmd/cbuild/commands/list/list_test.go b/cmd/cbuild/commands/list/list_test.go index c50555a..929c661 100644 --- a/cmd/cbuild/commands/list/list_test.go +++ b/cmd/cbuild/commands/list/list_test.go @@ -8,49 +8,15 @@ package list_test import ( "cbuild/cmd/cbuild/commands" - "os" - "runtime" "testing" - "time" - cp "github.com/otiai10/copy" "github.com/stretchr/testify/assert" ) const testRoot = "../../../../test" -func init() { - // 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) - - var binExtension string - if runtime.GOOS == "windows" { - binExtension = ".exe" - } - cbuildgenBin := testRoot + "/run/bin/cbuildgen" + binExtension - file, _ := os.Create(cbuildgenBin) - defer file.Close() - csolutionBin := testRoot + "/run/bin/csolution" + binExtension - file, _ = os.Create(csolutionBin) - defer file.Close() - cpackgetBin := testRoot + "/run/bin/cpackget" + binExtension - file, _ = os.Create(cpackgetBin) - defer file.Close() - - _ = cp.Copy(testRoot+"/run/test.Debug+CM0.cprj", testRoot+"/run/OutDir/test.Debug+CM0.cprj") -} - func TestListCommand(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") t.Run("No arguments", func(t *testing.T) { cmd := commands.NewRootCmd() diff --git a/cmd/cbuild/commands/list/list_toolchains_test.go b/cmd/cbuild/commands/list/list_toolchains_test.go index 75228ba..5f65b87 100644 --- a/cmd/cbuild/commands/list/list_toolchains_test.go +++ b/cmd/cbuild/commands/list/list_toolchains_test.go @@ -8,7 +8,6 @@ package list_test import ( "cbuild/cmd/cbuild/commands" - "os" "testing" "github.com/stretchr/testify/assert" @@ -16,7 +15,6 @@ import ( func TestListToolchainsCommand(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") csolutionFile := testRoot + "/run/TestSolution/test.csolution.yml" t.Run("invalid flag", func(t *testing.T) { diff --git a/cmd/cbuild/commands/root_test.go b/cmd/cbuild/commands/root_test.go index 0ac75c0..2badaec 100644 --- a/cmd/cbuild/commands/root_test.go +++ b/cmd/cbuild/commands/root_test.go @@ -8,12 +8,10 @@ package commands_test import ( "cbuild/cmd/cbuild/commands" + "cbuild/pkg/inittest" "os" - "runtime" "testing" - "time" - cp "github.com/otiai10/copy" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) @@ -21,37 +19,11 @@ import ( const testRoot = "../../../test" func init() { - // 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) - - var binExtension string - if runtime.GOOS == "windows" { - binExtension = ".exe" - } - cbuildgenBin := testRoot + "/run/bin/cbuildgen" + binExtension - file, _ := os.Create(cbuildgenBin) - defer file.Close() - csolutionBin := testRoot + "/run/bin/csolution" + binExtension - file, _ = os.Create(csolutionBin) - defer file.Close() - cpackgetBin := testRoot + "/run/bin/cpackget" + binExtension - file, _ = os.Create(cpackgetBin) - defer file.Close() - - _ = cp.Copy(testRoot+"/run/test.Debug+CM0.cprj", testRoot+"/run/OutDir/test.Debug+CM0.cprj") + inittest.TestInitialization(testRoot) } func TestCommands(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") cprjFile := testRoot + "/run/minimal.cprj" csolutionFile := testRoot + "/run/test.csolution.yml" @@ -100,7 +72,6 @@ func TestCommands(t *testing.T) { func TestPreLogConfiguration(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") logDir := testRoot + "/run/log" logFile := logDir + "/test.log" diff --git a/pkg/builder/cproject/builder_test.go b/pkg/builder/cproject/builder_test.go index 6088f5f..669e4ba 100644 --- a/pkg/builder/cproject/builder_test.go +++ b/pkg/builder/cproject/builder_test.go @@ -8,21 +8,24 @@ package cproject import ( "cbuild/pkg/builder" - utils "cbuild/pkg/utils" + "cbuild/pkg/inittest" + "cbuild/pkg/utils" "errors" "os" "path/filepath" "runtime" "strings" "testing" - "time" - cp "github.com/otiai10/copy" "github.com/stretchr/testify/assert" ) const testRoot = "../../../test" +func init() { + inittest.TestInitialization(testRoot) +} + type RunnerMock struct{} func (r RunnerMock) ExecuteCommand(program string, quiet bool, args ...string) (string, error) { @@ -46,28 +49,6 @@ func (r RunnerMock) ExecuteCommand(program string, quiet bool, args ...string) ( return "", nil } -func init() { - // 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) - - var binExtension string - if runtime.GOOS == "windows" { - binExtension = ".exe" - } - - cbuildgenBin := testRoot + "/run/bin/cbuildgen" + binExtension - file, _ := os.Create(cbuildgenBin) - defer file.Close() - cpackgetBin := testRoot + "/run/bin/cpackget" + binExtension - file, _ = os.Create(cpackgetBin) - defer file.Close() -} - func TestCheckCprj(t *testing.T) { assert := assert.New(t) @@ -195,27 +176,11 @@ func TestGetInternalVars(t *testing.T) { InputFile: testRoot + "/run/minimal.cprj", }, } - t.Run("test get internal vars, without CMSIS_BUILD_ROOT", func(t *testing.T) { + t.Run("test get internal vars", func(t *testing.T) { _, err := b.getInternalVars() assert.Error(err) }) - - t.Run("test get internal vars, with CMSIS_BUILD_ROOT", func(t *testing.T) { - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") - configs, err := utils.GetInstallConfigs() - assert.Nil(err) - b.InstallConfigs = configs - - vars, err := b.getInternalVars() - assert.Nil(err) - assert.NotEmpty(vars.cprjPath) - assert.NotEmpty(vars.cprjFilename) - assert.NotEmpty(vars.binPath) - assert.NotEmpty(vars.etcPath) - assert.NotEmpty(vars.cbuildgenBin) - assert.NotEmpty(vars.cpackgetBin) - }) } func TestGetJobs(t *testing.T) { @@ -247,9 +212,7 @@ func TestGetJobs(t *testing.T) { func TestBuild(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") - configs, err := utils.GetInstallConfigs() - assert.Nil(err) + configs := inittest.GetTestConfigs(testRoot) b := CprjBuilder{ builder.BuilderParams{ @@ -260,7 +223,11 @@ func TestBuild(t *testing.T) { OutDir: testRoot + "/run/OutDir", Packs: true, }, - InstallConfigs: configs, + InstallConfigs: utils.Configurations{ + BinPath: configs.BinPath, + BinExtn: configs.BinExtn, + EtcPath: configs.EtcPath, + }, }, } @@ -359,7 +326,6 @@ func TestBuildFail(t *testing.T) { }, }, } - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") t.Run("test build cprj without packs", func(t *testing.T) { b.Options.Packs = false diff --git a/pkg/builder/csolution/builder_test.go b/pkg/builder/csolution/builder_test.go index 35c929c..6d513ff 100644 --- a/pkg/builder/csolution/builder_test.go +++ b/pkg/builder/csolution/builder_test.go @@ -8,22 +8,23 @@ package csolution import ( builder "cbuild/pkg/builder" + "cbuild/pkg/inittest" "cbuild/pkg/utils" "errors" "os" "path/filepath" - "runtime" "strings" "testing" - "time" - - cp "github.com/otiai10/copy" "github.com/stretchr/testify/assert" ) const testRoot = "../../../test" +func init() { + inittest.TestInitialization(testRoot) +} + type RunnerMock struct{} func (r RunnerMock) ExecuteCommand(program string, quiet bool, args ...string) (string, error) { @@ -52,53 +53,26 @@ func (r RunnerMock) ExecuteCommand(program string, quiet bool, args ...string) ( return "", nil } -func init() { - // 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) - - var binExtension string - if runtime.GOOS == "windows" { - binExtension = ".exe" - } - cbuildgenBin := testRoot + "/run/bin/cbuildgen" + binExtension - file, _ := os.Create(cbuildgenBin) - defer file.Close() - csolutionBin := testRoot + "/run/bin/csolution" + binExtension - file, _ = os.Create(csolutionBin) - defer file.Close() - cpackgetBin := testRoot + "/run/bin/cpackget" + binExtension - file, _ = os.Create(cpackgetBin) - defer file.Close() - - _ = cp.Copy(testRoot+"/run/test.Debug+CM0.cprj", testRoot+"/run/OutDir/test.Debug+CM0.cprj") -} - func TestListContexts(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") - configs, err := utils.GetInstallConfigs() - assert.Nil(err) + configs := inittest.GetTestConfigs(testRoot) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ - Runner: RunnerMock{}, - InputFile: testRoot + "/run/test.csolution.yml", - InstallConfigs: configs, + Runner: RunnerMock{}, + InputFile: testRoot + "/run/TestSolution/test.csolution.yml", + InstallConfigs: utils.Configurations{ + BinPath: configs.BinPath, + BinExtn: configs.BinExtn, + EtcPath: configs.EtcPath, + }, }, } t.Run("test list contexts", func(t *testing.T) { contexts, err := b.listContexts(true, false) assert.Nil(err) - assert.Equal(len(contexts), 2) + assert.Equal(2, len(contexts)) assert.Equal("test.Debug+CM0", contexts[0]) assert.Equal("test.Release+CM0", contexts[1]) }) @@ -145,14 +119,16 @@ func TestListContexts(t *testing.T) { func TestListToolchians(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") - configs, err := utils.GetInstallConfigs() - assert.Nil(err) + configs := inittest.GetTestConfigs(testRoot) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ - Runner: RunnerMock{}, - InputFile: testRoot + "/run/test.csolution.yml", - InstallConfigs: configs, + Runner: RunnerMock{}, + InputFile: testRoot + "/run/test.csolution.yml", + InstallConfigs: utils.Configurations{ + BinPath: configs.BinPath, + BinExtn: configs.BinExtn, + EtcPath: configs.EtcPath, + }, }, } @@ -212,13 +188,15 @@ func TestListToolchians(t *testing.T) { func TestListEnvironment(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") - configs, err := utils.GetInstallConfigs() - assert.Nil(err) + configs := inittest.GetTestConfigs(testRoot) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ - Runner: RunnerMock{}, - InstallConfigs: configs, + Runner: RunnerMock{}, + InstallConfigs: utils.Configurations{ + BinPath: configs.BinPath, + BinExtn: configs.BinExtn, + EtcPath: configs.EtcPath, + }, }, } @@ -239,7 +217,11 @@ func TestListEnvironment(t *testing.T) { assert.Error(err) assert.Equal(len(envConfigs), 0) // restore install config - b.InstallConfigs = configs + b.InstallConfigs = utils.Configurations{ + BinPath: configs.BinPath, + BinExtn: configs.BinExtn, + EtcPath: configs.EtcPath, + } }) t.Run("test list environment", func(t *testing.T) { @@ -251,11 +233,8 @@ func TestListEnvironment(t *testing.T) { func TestBuild(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") os.Setenv("CMSIS_PACK_ROOT", testRoot+"/run/packs") - configs, err := utils.GetInstallConfigs() - assert.Nil(err) - + configs := inittest.GetTestConfigs(testRoot) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ Runner: RunnerMock{}, @@ -265,7 +244,11 @@ func TestBuild(t *testing.T) { OutDir: testRoot + "/run/OutDir", Packs: true, }, - InstallConfigs: configs, + InstallConfigs: utils.Configurations{ + BinPath: configs.BinPath, + BinExtn: configs.BinExtn, + EtcPath: configs.EtcPath, + }, }, } @@ -283,19 +266,21 @@ func TestBuild(t *testing.T) { func TestInstallMissingPacks(t *testing.T) { assert := assert.New(t) - os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") - configs, err := utils.GetInstallConfigs() - assert.Nil(err) + configs := inittest.GetTestConfigs(testRoot) b := CSolutionBuilder{ BuilderParams: builder.BuilderParams{ - Runner: RunnerMock{}, - InstallConfigs: configs, + Runner: RunnerMock{}, + InstallConfigs: utils.Configurations{ + BinPath: configs.BinPath, + BinExtn: configs.BinExtn, + EtcPath: configs.EtcPath, + }, }, } t.Run("test install missing packs", func(t *testing.T) { - err = b.installMissingPacks() + err := b.installMissingPacks() assert.Nil(err) }) diff --git a/pkg/inittest/inittest.go b/pkg/inittest/inittest.go new file mode 100644 index 0000000..874f550 --- /dev/null +++ b/pkg/inittest/inittest.go @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Arm Limited. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +// This package is used as a common test setup +// avoiding duplicate setup for all the packages +// under test + +package inittest + +import ( + "os" + "path/filepath" + "runtime" + "time" + + cp "github.com/otiai10/copy" +) + +func TestInitialization(testRoot string) { + // 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) + + var binExtension string + if runtime.GOOS == "windows" { + binExtension = ".exe" + } + cbuildgenBin := testRoot + "/run/bin/cbuildgen" + binExtension + file, _ := os.Create(cbuildgenBin) + defer file.Close() + csolutionBin := testRoot + "/run/bin/csolution" + binExtension + file, _ = os.Create(csolutionBin) + defer file.Close() + cpackgetBin := testRoot + "/run/bin/cpackget" + binExtension + file, _ = os.Create(cpackgetBin) + defer file.Close() +} + +type TestConfigs struct { + BinPath string + EtcPath string + BinExtn string +} + +func GetTestConfigs(testRoot string) (configs TestConfigs) { + if runtime.GOOS == "windows" { + configs.BinExtn = ".exe" + } + configs.BinPath, _ = filepath.Abs(testRoot + "/run/bin") + configs.EtcPath, _ = filepath.Abs(testRoot + "/run/etc") + return configs +} diff --git a/pkg/utils/configs.go b/pkg/utils/configs.go index 124ddc8..3d8b5e9 100644 --- a/pkg/utils/configs.go +++ b/pkg/utils/configs.go @@ -23,12 +23,9 @@ func GetInstallConfigs() (configs Configurations, err error) { if runtime.GOOS == "windows" { configs.BinExtn = ".exe" } - binPath := os.Getenv("CMSIS_BUILD_ROOT") - if binPath == "" { - binPath, err = GetExecutablePath() - if err != nil { - return Configurations{}, err - } + binPath, err := GetExecutablePath() + if err != nil { + return Configurations{}, err } if binPath != "" { binPath, _ = filepath.Abs(binPath) diff --git a/pkg/utils/configs_test.go b/pkg/utils/configs_test.go index 3a736a9..e437d6d 100644 --- a/pkg/utils/configs_test.go +++ b/pkg/utils/configs_test.go @@ -7,45 +7,19 @@ package utils import ( - "os" - "runtime" + "cbuild/pkg/inittest" "testing" - "time" - - cp "github.com/otiai10/copy" "github.com/stretchr/testify/assert" ) func init() { - // Prepare test data - _ = os.RemoveAll(testRoot + "/run") - time.Sleep(time.Second) - _ = os.MkdirAll(testRoot+"/run/bin", 0755) - _ = os.MkdirAll(testRoot+"/run/etc", 0755) - _ = cp.Copy(testRoot+"/data/Test.cbuild-idx.yml", testRoot+"/run/Test.cbuild-idx.yml") + inittest.TestInitialization(testRoot) } - func TestGetInstallConfigs(t *testing.T) { assert := assert.New(t) - t.Run("test get install configs with CMSIS_BUILD_ROOT", func(t *testing.T) { - err := os.Setenv("CMSIS_BUILD_ROOT", testRoot+"/run/bin") - assert.Nil(err) - configs, err := GetInstallConfigs() - assert.Nil(err) - assert.NotEmpty(configs.BinPath) - assert.NotEmpty(configs.EtcPath) - if runtime.GOOS == "windows" { - assert.NotEmpty(configs.BinExtn) - } else { - assert.Empty(configs.BinExtn) - } - }) - - t.Run("test get install configurations without CMSIS_BUILD_ROOT", func(t *testing.T) { - err := os.Unsetenv("CMSIS_BUILD_ROOT") - assert.Nil(err) - _, err = GetInstallConfigs() + t.Run("test get install configurations", func(t *testing.T) { + _, err := GetInstallConfigs() assert.Error(err) }) } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index ad6532c..e99ae75 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -57,14 +57,9 @@ func UpdateEnvVars(binPath string, etcPath string) (env EnvVars) { env.CompilerRoot, _ = filepath.Abs(etcPath) os.Setenv("CMSIS_COMPILER_ROOT", env.CompilerRoot) } - env.BuildRoot = os.Getenv("CMSIS_BUILD_ROOT") - if env.BuildRoot == "" { - env.BuildRoot, _ = filepath.Abs(binPath) - os.Setenv("CMSIS_BUILD_ROOT", env.BuildRoot) - } + env.BuildRoot, _ = filepath.Abs(binPath) log.Debug("CMSIS_PACK_ROOT: " + env.PackRoot) log.Debug("CMSIS_COMPILER_ROOT: " + env.CompilerRoot) - log.Debug("CMSIS_BUILD_ROOT: " + env.BuildRoot) return env }