From 5921b7ddc0430b2b8546ad067b9c8d369e8d4f54 Mon Sep 17 00:00:00 2001 From: mirkobrombin Date: Thu, 17 Aug 2023 11:27:54 +0200 Subject: [PATCH] fix: unit test --- cmd/pkg.go | 2 +- core/packages.go | 41 +++++++++++++++++++++++++---------------- core/system.go | 4 ++-- settings/config.go | 3 ++- tests/pkg_test.go | 2 +- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/cmd/pkg.go b/cmd/pkg.go index 8b4828b9..21360438 100644 --- a/cmd/pkg.go +++ b/cmd/pkg.go @@ -46,7 +46,7 @@ func pkg(cmd *cobra.Command, args []string) error { return nil } - pkgM := core.NewPackageManager() + pkgM := core.NewPackageManager(false) switch args[0] { case "add": diff --git a/core/packages.go b/core/packages.go index 4f9abb66..90f28275 100644 --- a/core/packages.go +++ b/core/packages.go @@ -25,13 +25,17 @@ import ( ) // PackageManager struct -type PackageManager struct{} +type PackageManager struct { + dryRun bool + baseDir string +} const ( - PackagesBaseDir = "/etc/abroot" - PackagesAddFile = "packages.add" - PackagesRemoveFile = "packages.remove" - PackagesUnstagedFile = "packages.unstaged" + PackagesBaseDir = "/etc/abroot" + DryRunPackagesBaseDir = "/tmp/abroot" + PackagesAddFile = "packages.add" + PackagesRemoveFile = "packages.remove" + PackagesUnstagedFile = "packages.unstaged" ) const ( @@ -50,19 +54,24 @@ type UnstagedPackage struct { } // NewPackageManager returns a new PackageManager struct -func NewPackageManager() *PackageManager { +func NewPackageManager(dryRun bool) *PackageManager { PrintVerbose("PackageManager.NewPackageManager: running...") - err := os.MkdirAll(PackagesBaseDir, 0755) + baseDir := PackagesBaseDir + if dryRun { + baseDir = DryRunPackagesBaseDir + } + + err := os.MkdirAll(baseDir, 0755) if err != nil { PrintVerbose("PackageManager.NewPackageManager:err: " + err.Error()) panic(err) } - _, err = os.Stat(filepath.Join(PackagesBaseDir, PackagesAddFile)) + _, err = os.Stat(filepath.Join(baseDir, PackagesAddFile)) if err != nil { err = os.WriteFile( - filepath.Join(PackagesBaseDir, PackagesAddFile), + filepath.Join(baseDir, PackagesAddFile), []byte(""), 0644, ) @@ -72,10 +81,10 @@ func NewPackageManager() *PackageManager { } } - _, err = os.Stat(filepath.Join(PackagesBaseDir, PackagesRemoveFile)) + _, err = os.Stat(filepath.Join(baseDir, PackagesRemoveFile)) if err != nil { err = os.WriteFile( - filepath.Join(PackagesBaseDir, PackagesRemoveFile), + filepath.Join(baseDir, PackagesRemoveFile), []byte(""), 0644, ) @@ -85,10 +94,10 @@ func NewPackageManager() *PackageManager { } } - _, err = os.Stat(filepath.Join(PackagesBaseDir, PackagesUnstagedFile)) + _, err = os.Stat(filepath.Join(baseDir, PackagesUnstagedFile)) if err != nil { err = os.WriteFile( - filepath.Join(PackagesBaseDir, PackagesUnstagedFile), + filepath.Join(baseDir, PackagesUnstagedFile), []byte(""), 0644, ) @@ -98,7 +107,7 @@ func NewPackageManager() *PackageManager { } } - return &PackageManager{} + return &PackageManager{dryRun, baseDir} } // Add adds a package to the packages.add file @@ -254,7 +263,7 @@ func (p *PackageManager) getPackages(file string) ([]string, error) { PrintVerbose("PackageManager.getPackages: running...") pkgs := []string{} - f, err := os.Open(filepath.Join(PackagesBaseDir, file)) + f, err := os.Open(filepath.Join(p.baseDir, file)) if err != nil { PrintVerbose("PackageManager.getPackages:err: " + err.Error()) return pkgs, err @@ -314,7 +323,7 @@ func (p *PackageManager) writeUnstagedPackages(pkgs []UnstagedPackage) error { func (p *PackageManager) writePackages(file string, pkgs []string) error { PrintVerbose("PackageManager.writePackages: running...") - f, err := os.Create(filepath.Join(PackagesBaseDir, file)) + f, err := os.Create(filepath.Join(p.baseDir, file)) if err != nil { PrintVerbose("PackageManager.writePackages:err: " + err.Error()) return err diff --git a/core/system.go b/core/system.go index 73c765a8..4c9b2003 100644 --- a/core/system.go +++ b/core/system.go @@ -255,7 +255,7 @@ func (s *ABSystem) RunCleanUpQueue(fnName string) error { } case "clearUnstagedPackages": PrintVerbose("ABSystem.RunCleanUpQueue: Executing clearUnstagedPackages") - pkgM := NewPackageManager() + pkgM := NewPackageManager(false) err := pkgM.ClearUnstagedPackages() if err != nil { PrintVerbose("ABSystem.RunCleanUpQueue:err(7): %s", err) @@ -579,7 +579,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error { "ABRoot.root": futurePartition.Label, } args := map[string]string{} - pkgM := NewPackageManager() + pkgM := NewPackageManager(false) pkgsFinal := pkgM.GetFinalCmd(operation) if pkgsFinal == "" { pkgsFinal = "true" diff --git a/settings/config.go b/settings/config.go index 06580627..260f1d9c 100644 --- a/settings/config.go +++ b/settings/config.go @@ -62,6 +62,7 @@ func init() { // dev paths viper.AddConfigPath("config/") + viper.AddConfigPath("../config/") // prod paths viper.AddConfigPath("/etc/abroot/") @@ -94,7 +95,7 @@ func init() { IPkgMngPost: viper.GetString("iPkgMngPost"), IPkgMngAdd: viper.GetString("iPkgMngAdd"), IPkgMngRm: viper.GetString("iPkgMngRm"), - IPkgMngApi: viper.GetString("IPkgMngApi"), + IPkgMngApi: viper.GetString("iPkgMngApi"), // Partitions PartLabelVar: viper.GetString("partLabelVar"), diff --git a/tests/pkg_test.go b/tests/pkg_test.go index e4fb82e9..a83d0255 100644 --- a/tests/pkg_test.go +++ b/tests/pkg_test.go @@ -8,7 +8,7 @@ import ( ) func TestPackageManager(t *testing.T) { - pm := core.NewPackageManager() + pm := core.NewPackageManager(true) // Add a package pkg := "bash htop"