Skip to content

[fileutil] Update IsDirEmpty, add unit tests #2646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
90 changes: 90 additions & 0 deletions internal/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
8 changes: 4 additions & 4 deletions internal/nix/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
79 changes: 79 additions & 0 deletions internal/nix/install_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Loading