Skip to content

Commit

Permalink
add and improve tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed May 13, 2024
1 parent 0435504 commit 2b18904
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func initConfig(cmd *cobra.Command) error {
sess.App.Version = version
sess.App.SemVer = semver

if err := session.CreateDefaultConfigIfMissing(configRoot); err != nil {
if _, err := session.CreateDefaultConfigIfMissing(configRoot); err != nil {
fmt.Printf("can't create default session: %v\n", err)

os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@ func generateJSON(results *findHostsResults) (json.RawMessage, error) {
return out, nil
}

func New(config *session.Session) (Processor, error) {
func New(sess *session.Session) (Processor, error) {
p := Processor{
Session: config,
Session: sess,
}

return p, nil
Expand Down
15 changes: 15 additions & 0 deletions process/process_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
package process

import (
"github.com/jonhadfield/ipscout/session"
"github.com/stretchr/testify/require"
"testing"
)

func TestNew(t *testing.T) {
t.Run("New", func(t *testing.T) {
n, err := New(session.New())
require.NoError(t, err)
require.NotNil(t, n)
})

}
23 changes: 11 additions & 12 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,40 +211,39 @@ func unmarshalConfig(data []byte) (*Session, error) {
return &conf, nil
}

func CreateDefaultConfigIfMissing(path string) error {
// CreateDefaultConfigIfMissing creates a default session configuration file if it does not exist
// and returns true if it was created, or false if it already exists
func CreateDefaultConfigIfMissing(path string) (bool, error) {
if path == "" {
return fmt.Errorf("session path not specified")
return false, fmt.Errorf("session path not specified")
}

var err error

// check if session already exists
_, err = os.Stat(filepath.Join(path, DefaultConfigFileName))

_, err := os.Stat(filepath.Join(path, DefaultConfigFileName))
switch {
case err == nil:
return nil
return false, nil
case os.IsNotExist(err):
// check default session is valid
if _, err = unmarshalConfig([]byte(defaultConfig)); err != nil {
return fmt.Errorf("default session invalid: %w", err)
return false, fmt.Errorf("default session invalid: %w", err)
}

// create dir specified in path argument if missing
if _, err = os.Stat(path); os.IsNotExist(err) {
if err = os.MkdirAll(path, 0o700); err != nil {
return fmt.Errorf("failed to create session directory: %w", err)
return false, fmt.Errorf("failed to create session directory: %w", err)
}
}

if err = os.WriteFile(filepath.Join(path, DefaultConfigFileName), []byte(defaultConfig), 0o600); err != nil {
return fmt.Errorf("failed to write default session: %w", err)
return false, fmt.Errorf("failed to write default session: %w", err)
}
case err != nil:
return fmt.Errorf("failed to stat session directory: %w", err)
return false, fmt.Errorf("failed to stat session directory: %w", err)
}

return nil
return true, nil
}

// CreateConfigPathStructure creates all the necessary paths under session root if they don't exist
Expand Down
23 changes: 15 additions & 8 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,25 @@ func TestUnmarshalConfig(t *testing.T) {
func TestCreateDefaultConfig(t *testing.T) {
t.Run("PathExists", func(t *testing.T) {
path := "/tmp"
err := CreateDefaultConfigIfMissing(path)
created, err := CreateDefaultConfigIfMissing(path)
require.NoError(t, err)
require.False(t, created)
})

t.Run("PathDoesNotExist", func(t *testing.T) {
path := "/tmp/nonexistent"
err := CreateDefaultConfigIfMissing(path)
path := t.TempDir()
created, err := CreateDefaultConfigIfMissing(path)
require.NoError(t, err)
require.True(t, created)
_, err = os.Stat(path)
require.NoError(t, err)
})

t.Run("InvalidPath", func(t *testing.T) {
path := ""
err := CreateDefaultConfigIfMissing(path)
created, err := CreateDefaultConfigIfMissing(path)
require.Error(t, err)
require.False(t, created)
})
}

Expand All @@ -65,10 +68,12 @@ func TestCreateCachePathIfNotExist(t *testing.T) {
configRoot := GetConfigRoot(tempDir, AppName)

// create session root (required for cache path)
require.NoError(t, CreateDefaultConfigIfMissing(configRoot))
created, err := CreateDefaultConfigIfMissing(configRoot)
require.NoError(t, err)
require.True(t, created)

// check session root exists
_, err := os.Stat(configRoot)
_, err = os.Stat(configRoot)
require.NoError(t, err)

// check cache path does not exist
Expand All @@ -89,9 +94,11 @@ func TestCreateCachePathIfNotExist(t *testing.T) {
configRoot := GetConfigRoot(tempDir, AppName)

// create session root (required for cache path)
require.NoError(t, CreateDefaultConfigIfMissing(configRoot))
created, err := CreateDefaultConfigIfMissing(configRoot)
require.NoError(t, err)
require.True(t, created)

err := CreateConfigPathStructure(configRoot)
err = CreateConfigPathStructure(configRoot)
require.NoError(t, err)

for _, dir := range []string{"backups", "cache"} {
Expand Down

0 comments on commit 2b18904

Please sign in to comment.