mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat: Config Persistence & API (#83)
* introduce a persistent config * add tests and refactor config creation * update config handling and add support for removing config * add support to skip suspicious pkgs marked as trusted * add support for config dir Env & unexport functions * small fixes * add assert for dir * fix tests * fix shell source line & trusted pkgs parsing * fix flag inconsistency * update config to read on each invocation and create if does not exist * fix flags value being overridden * remove redundant func call * modify trusted pkg check to be config bound * modify RemoveConfig to rm files & not dir. add tests for paths.go * add versions for package for e2e * modify tests to reset config * fix: Simplify config persistence * fix: Misc comments * fix: Misc fix * fix: Do not overwrite config file if exists * fix: Do not overwrite config file if exists * fix: Config cobra command should override and not replace * fix: Create dir before writing config template * fix: Create dir before writing config template * fix: Misc refactoring * test: Add test for is trusted package version * Update cmd/setup/setup.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com> * Update config/config.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com> * fix: Remove unused constant in config * fix: Resolve conflict with event logger * docs: Add doc for eventlogger.Logger interface * test: Add E2E for config file creation * fix: Code review fixes --------- Signed-off-by: Abhisek Datta <abhisek.datta@gmail.com> Co-authored-by: Sahilb315 <bansalsahil315@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
Sahilb315
parent
698bd3dd13
commit
20c854e473
@@ -7,29 +7,22 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetDefaultLogDir(t *testing.T) {
|
||||
logDir, err := GetDefaultLogDir()
|
||||
if err != nil {
|
||||
t.Fatalf("GetDefaultLogDir() failed: %v", err)
|
||||
}
|
||||
assert.NoError(t, err, "failed to get default log directory")
|
||||
|
||||
if logDir == "" {
|
||||
t.Error("Expected non-empty log directory")
|
||||
}
|
||||
|
||||
// Check that it contains expected path components
|
||||
expectedDir := ".pmg"
|
||||
if filepath.Base(filepath.Dir(logDir)) != expectedDir && filepath.Base(filepath.Dir(filepath.Dir(logDir))) != expectedDir {
|
||||
t.Errorf("Expected log directory to contain %s, got: %s", expectedDir, logDir)
|
||||
}
|
||||
assert.NotEmpty(t, logDir, "log directory should not be empty")
|
||||
assert.Contains(t, logDir, "safedep/pmg/logs")
|
||||
}
|
||||
|
||||
func TestLoggerInitialization(t *testing.T) {
|
||||
// Create a temporary directory for testing
|
||||
tmpDir := t.TempDir()
|
||||
logDir := filepath.Join(tmpDir, ".pmg", "logs")
|
||||
logDir := filepath.Join(tmpDir, "pmg", "logs")
|
||||
|
||||
// Initialize logger
|
||||
err := InitializeWithDir(logDir)
|
||||
@@ -151,7 +144,7 @@ func TestInitializeWithFile(t *testing.T) {
|
||||
if err == nil {
|
||||
Close()
|
||||
}
|
||||
|
||||
|
||||
// Reset for custom file
|
||||
once = sync.Once{}
|
||||
err = InitializeWithFile(logFile)
|
||||
@@ -231,11 +224,12 @@ func TestCleanupOldLogs(t *testing.T) {
|
||||
}
|
||||
|
||||
// Initialize logger (which triggers cleanup)
|
||||
logger := &Logger{}
|
||||
logger := &fileWithRotationLogger{}
|
||||
err = logger.init(logDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize logger: %v", err)
|
||||
}
|
||||
|
||||
defer logger.Close()
|
||||
|
||||
// Give cleanup goroutine time to run
|
||||
@@ -251,4 +245,3 @@ func TestCleanupOldLogs(t *testing.T) {
|
||||
t.Error("Recent log file should still exist")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user