Skip to content

Commit

Permalink
fix: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Aug 17, 2023
1 parent d3f9a4b commit 5921b7d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
41 changes: 25 additions & 16 deletions core/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -98,7 +107,7 @@ func NewPackageManager() *PackageManager {
}
}

return &PackageManager{}
return &PackageManager{dryRun, baseDir}
}

// Add adds a package to the packages.add file
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion settings/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func init() {

// dev paths
viper.AddConfigPath("config/")
viper.AddConfigPath("../config/")

// prod paths
viper.AddConfigPath("/etc/abroot/")
Expand Down Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion tests/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestPackageManager(t *testing.T) {
pm := core.NewPackageManager()
pm := core.NewPackageManager(true)

// Add a package
pkg := "bash htop"
Expand Down

0 comments on commit 5921b7d

Please sign in to comment.