Skip to content

Commit 0859362

Browse files
authored
[fileutil] Update IsDirEmpty, add unit tests (#2646)
## Summary Adds error return to `IsDirEmpty` and unit tests ## How was it tested? CICD ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request, I represent that I have the right to license the contributions to the project maintainers under the Apache 2 License as stated in the [Community Contribution License](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license).
1 parent 8237a2a commit 0859362

File tree

4 files changed

+176
-7
lines changed

4 files changed

+176
-7
lines changed

internal/fileutil/fileutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func Exists(path string) bool {
5151
return err == nil
5252
}
5353

54-
func IsDirEmpty(path string) bool {
54+
func IsDirEmpty(path string) (bool, error) {
5555
entries, err := os.ReadDir(path)
5656
if err != nil {
57-
return false
57+
return false, err
5858
}
59-
return len(entries) == 0
59+
return len(entries) == 0, nil
6060
}
6161

6262
// FileContains checks if a given file at 'path' contains the 'substring'

internal/fileutil/fileutil_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
2+
// Use of this source code is governed by the license in the LICENSE file.
3+
4+
package fileutil
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestIsDirEmpty(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
setup func(string) error
19+
expected bool
20+
wantErr bool
21+
}{
22+
{
23+
name: "empty directory",
24+
setup: func(dir string) error {
25+
return nil // Directory is already empty
26+
},
27+
expected: true,
28+
wantErr: false,
29+
},
30+
{
31+
name: "directory with files",
32+
setup: func(dir string) error {
33+
file := filepath.Join(dir, "test.txt")
34+
return os.WriteFile(file, []byte("test content"), 0o644)
35+
},
36+
expected: false,
37+
wantErr: false,
38+
},
39+
{
40+
name: "directory with subdirectories",
41+
setup: func(dir string) error {
42+
subdir := filepath.Join(dir, "subdir")
43+
return os.MkdirAll(subdir, 0o755)
44+
},
45+
expected: false,
46+
wantErr: false,
47+
},
48+
{
49+
name: "directory with hidden files",
50+
setup: func(dir string) error {
51+
file := filepath.Join(dir, ".hidden")
52+
return os.WriteFile(file, []byte("hidden content"), 0o644)
53+
},
54+
expected: false,
55+
wantErr: false,
56+
},
57+
{
58+
name: "non-existent directory",
59+
setup: func(dir string) error {
60+
return os.RemoveAll(dir)
61+
},
62+
expected: false,
63+
wantErr: true,
64+
},
65+
}
66+
67+
for _, curTest := range tests {
68+
t.Run(curTest.name, func(t *testing.T) {
69+
// Create temporary directory for test
70+
tempDir := t.TempDir()
71+
72+
// Setup test case
73+
if curTest.setup != nil {
74+
err := curTest.setup(tempDir)
75+
require.NoError(t, err)
76+
}
77+
78+
// Run the function
79+
isEmpty, err := IsDirEmpty(tempDir)
80+
81+
// Check results
82+
if curTest.wantErr {
83+
assert.Error(t, err)
84+
} else {
85+
assert.NoError(t, err)
86+
assert.Equal(t, curTest.expected, isEmpty)
87+
}
88+
})
89+
}
90+
}

internal/nix/install.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ func BinaryInstalled() bool {
2525
return cmdutil.Exists("nix")
2626
}
2727

28-
func dirExistsAndIsNotEmpty() bool {
29-
dir := "/nix"
30-
return fileutil.Exists(dir) && !fileutil.IsDirEmpty(dir)
28+
func dirExistsAndIsNotEmpty(dir string) bool {
29+
empty, err := fileutil.IsDirEmpty(dir)
30+
return err == nil && !empty
3131
}
3232

3333
var ensured = false
@@ -58,7 +58,7 @@ func EnsureNixInstalled(ctx context.Context, writer io.Writer, withDaemonFunc fu
5858
if BinaryInstalled() {
5959
return nil
6060
}
61-
if dirExistsAndIsNotEmpty() {
61+
if dirExistsAndIsNotEmpty("/nix") {
6262
if _, err = SourceProfile(); err != nil {
6363
return err
6464
} else if BinaryInstalled() {

internal/nix/install_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
2+
// Use of this source code is governed by the license in the LICENSE file.
3+
4+
package nix
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestDirExistsAndIsNotEmpty(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
setup func(string) error
19+
expected bool
20+
}{
21+
{
22+
name: "empty directory",
23+
setup: func(dir string) error {
24+
return nil // Directory is already empty
25+
},
26+
expected: false,
27+
},
28+
{
29+
name: "directory with files",
30+
setup: func(dir string) error {
31+
file := filepath.Join(dir, "test.txt")
32+
return os.WriteFile(file, []byte("test content"), 0o644)
33+
},
34+
expected: true,
35+
},
36+
{
37+
name: "directory with subdirectories",
38+
setup: func(dir string) error {
39+
subdir := filepath.Join(dir, "subdir")
40+
return os.MkdirAll(subdir, 0o755)
41+
},
42+
expected: true,
43+
},
44+
{
45+
name: "directory with hidden files",
46+
setup: func(dir string) error {
47+
file := filepath.Join(dir, ".hidden")
48+
return os.WriteFile(file, []byte("hidden content"), 0o644)
49+
},
50+
expected: true,
51+
},
52+
{
53+
name: "non-existent directory",
54+
setup: func(dir string) error {
55+
return os.RemoveAll(dir)
56+
},
57+
expected: false,
58+
},
59+
}
60+
61+
for _, curTest := range tests {
62+
t.Run(curTest.name, func(t *testing.T) {
63+
// Create temporary directory for test
64+
tempDir := t.TempDir()
65+
66+
// Setup test case
67+
if curTest.setup != nil {
68+
err := curTest.setup(tempDir)
69+
require.NoError(t, err)
70+
}
71+
72+
// Run the function
73+
result := dirExistsAndIsNotEmpty(tempDir)
74+
75+
// Check results
76+
assert.Equal(t, curTest.expected, result)
77+
})
78+
}
79+
}

0 commit comments

Comments
 (0)