diff --git a/internal/fileutil/fileutil.go b/internal/fileutil/fileutil.go index 7e63b0f42d5..5cd40aa23d3 100644 --- a/internal/fileutil/fileutil.go +++ b/internal/fileutil/fileutil.go @@ -51,12 +51,12 @@ func Exists(path string) bool { return err == nil } -func IsDirEmpty(path string) bool { +func IsDirEmpty(path string) (bool, error) { entries, err := os.ReadDir(path) if err != nil { - return false + return false, err } - return len(entries) == 0 + return len(entries) == 0, nil } // FileContains checks if a given file at 'path' contains the 'substring' diff --git a/internal/fileutil/fileutil_test.go b/internal/fileutil/fileutil_test.go new file mode 100644 index 00000000000..766dfc287d3 --- /dev/null +++ b/internal/fileutil/fileutil_test.go @@ -0,0 +1,90 @@ +// Copyright 2024 Jetify Inc. and contributors. All rights reserved. +// Use of this source code is governed by the license in the LICENSE file. + +package fileutil + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestIsDirEmpty(t *testing.T) { + tests := []struct { + name string + setup func(string) error + expected bool + wantErr bool + }{ + { + name: "empty directory", + setup: func(dir string) error { + return nil // Directory is already empty + }, + expected: true, + wantErr: false, + }, + { + name: "directory with files", + setup: func(dir string) error { + file := filepath.Join(dir, "test.txt") + return os.WriteFile(file, []byte("test content"), 0o644) + }, + expected: false, + wantErr: false, + }, + { + name: "directory with subdirectories", + setup: func(dir string) error { + subdir := filepath.Join(dir, "subdir") + return os.MkdirAll(subdir, 0o755) + }, + expected: false, + wantErr: false, + }, + { + name: "directory with hidden files", + setup: func(dir string) error { + file := filepath.Join(dir, ".hidden") + return os.WriteFile(file, []byte("hidden content"), 0o644) + }, + expected: false, + wantErr: false, + }, + { + name: "non-existent directory", + setup: func(dir string) error { + return os.RemoveAll(dir) + }, + expected: false, + wantErr: true, + }, + } + + for _, curTest := range tests { + t.Run(curTest.name, func(t *testing.T) { + // Create temporary directory for test + tempDir := t.TempDir() + + // Setup test case + if curTest.setup != nil { + err := curTest.setup(tempDir) + require.NoError(t, err) + } + + // Run the function + isEmpty, err := IsDirEmpty(tempDir) + + // Check results + if curTest.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, curTest.expected, isEmpty) + } + }) + } +} diff --git a/internal/nix/install.go b/internal/nix/install.go index bcdeff7fe58..d714b8b5646 100644 --- a/internal/nix/install.go +++ b/internal/nix/install.go @@ -25,9 +25,9 @@ func BinaryInstalled() bool { return cmdutil.Exists("nix") } -func dirExistsAndIsNotEmpty() bool { - dir := "/nix" - return fileutil.Exists(dir) && !fileutil.IsDirEmpty(dir) +func dirExistsAndIsNotEmpty(dir string) bool { + empty, err := fileutil.IsDirEmpty(dir) + return err == nil && !empty } var ensured = false @@ -58,7 +58,7 @@ func EnsureNixInstalled(ctx context.Context, writer io.Writer, withDaemonFunc fu if BinaryInstalled() { return nil } - if dirExistsAndIsNotEmpty() { + if dirExistsAndIsNotEmpty("/nix") { if _, err = SourceProfile(); err != nil { return err } else if BinaryInstalled() { diff --git a/internal/nix/install_test.go b/internal/nix/install_test.go new file mode 100644 index 00000000000..7e8250849c3 --- /dev/null +++ b/internal/nix/install_test.go @@ -0,0 +1,79 @@ +// Copyright 2024 Jetify Inc. and contributors. All rights reserved. +// Use of this source code is governed by the license in the LICENSE file. + +package nix + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestDirExistsAndIsNotEmpty(t *testing.T) { + tests := []struct { + name string + setup func(string) error + expected bool + }{ + { + name: "empty directory", + setup: func(dir string) error { + return nil // Directory is already empty + }, + expected: false, + }, + { + name: "directory with files", + setup: func(dir string) error { + file := filepath.Join(dir, "test.txt") + return os.WriteFile(file, []byte("test content"), 0o644) + }, + expected: true, + }, + { + name: "directory with subdirectories", + setup: func(dir string) error { + subdir := filepath.Join(dir, "subdir") + return os.MkdirAll(subdir, 0o755) + }, + expected: true, + }, + { + name: "directory with hidden files", + setup: func(dir string) error { + file := filepath.Join(dir, ".hidden") + return os.WriteFile(file, []byte("hidden content"), 0o644) + }, + expected: true, + }, + { + name: "non-existent directory", + setup: func(dir string) error { + return os.RemoveAll(dir) + }, + expected: false, + }, + } + + for _, curTest := range tests { + t.Run(curTest.name, func(t *testing.T) { + // Create temporary directory for test + tempDir := t.TempDir() + + // Setup test case + if curTest.setup != nil { + err := curTest.setup(tempDir) + require.NoError(t, err) + } + + // Run the function + result := dirExistsAndIsNotEmpty(tempDir) + + // Check results + assert.Equal(t, curTest.expected, result) + }) + } +}