/ lnutils / fs_test.go
fs_test.go
 1  package lnutils
 2  
 3  import (
 4  	"os"
 5  	"path/filepath"
 6  	"testing"
 7  
 8  	"github.com/stretchr/testify/require"
 9  )
10  
11  // TestCreateDir verifies the behavior of CreateDir function in various
12  // scenarios:
13  // - Creating a new directory when it doesn't exist
14  // - Handling an already existing directory
15  // - Dealing with symlinks pointing to non-existent directories
16  // - Handling invalid paths
17  // The test uses a temporary directory and runs multiple test cases to ensure
18  // proper directory creation, permission settings, and error handling.
19  func TestCreateDir(t *testing.T) {
20  	t.Parallel()
21  
22  	tempDir := t.TempDir()
23  
24  	tests := []struct {
25  		name      string
26  		setup     func() string
27  		wantError bool
28  	}{
29  		{
30  			name: "create directory",
31  			setup: func() string {
32  				return filepath.Join(tempDir, "testdir")
33  			},
34  			wantError: false,
35  		},
36  		{
37  			name: "existing directory",
38  			setup: func() string {
39  				dir := filepath.Join(tempDir, "testdir")
40  				err := os.Mkdir(dir, 0700)
41  				require.NoError(t, err)
42  
43  				return dir
44  			},
45  			wantError: false,
46  		},
47  		{
48  			name: "symlink to non-existent directory",
49  			setup: func() string {
50  				dir := filepath.Join(tempDir, "testdir")
51  				symlink := filepath.Join(tempDir, "symlink")
52  				err := os.Symlink(dir, symlink)
53  				require.NoError(t, err)
54  
55  				return symlink
56  			},
57  			wantError: true,
58  		},
59  		{
60  			name: "invalid path",
61  			setup: func() string {
62  				return string([]byte{0})
63  			},
64  			wantError: true,
65  		},
66  	}
67  
68  	for _, tc := range tests {
69  		tc := tc
70  		t.Run(tc.name, func(t *testing.T) {
71  			dir := tc.setup()
72  			defer os.RemoveAll(dir)
73  
74  			err := CreateDir(dir, 0700)
75  			if tc.wantError {
76  				require.Error(t, err)
77  				return
78  			}
79  
80  			require.NoError(t, err)
81  
82  			info, err := os.Stat(dir)
83  			require.NoError(t, err)
84  			require.True(t, info.IsDir())
85  		})
86  	}
87  }