-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: restructure code to enhance testability
- Loading branch information
Showing
7 changed files
with
101 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package fspath | ||
|
||
import ( | ||
"io/fs" | ||
) | ||
|
||
const ( | ||
default_dir_permissons_code = 0700 | ||
) | ||
|
||
type FsCreator interface { | ||
MkdirAll(path string, perm fs.FileMode) error | ||
Create(name string) error | ||
} | ||
|
||
// CreateFile creates all missing parent dir's in the file and the file itself. | ||
func CreateFile(creator FsCreator, path string) error { | ||
pathComponents := getPathComponents(path) | ||
err := creator.MkdirAll(pathComponents.DirPath, default_dir_permissons_code) | ||
if err != nil { | ||
return err | ||
} | ||
err = creator.Create(path) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package fspath | ||
|
||
import ( | ||
"io/fs" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type fakeFsCreator struct { | ||
fs *afero.MemMapFs | ||
} | ||
|
||
func (rc *fakeFsCreator) MkdirAll(path string, perm fs.FileMode) error { | ||
return rc.fs.MkdirAll(path, perm) | ||
} | ||
|
||
func (rc *fakeFsCreator) Create(name string) error { | ||
_, err := rc.fs.Create(name) | ||
return err | ||
} | ||
|
||
func TestFsCreator(t *testing.T) { | ||
cases := map[string]string{ | ||
"simple file": "./exampleDir/example.txt", | ||
"dotfile, relative to user dir": "~/.config/.vimrc", | ||
"multi-dot-file, deeply nested": "./test/test/test/testing.dev.env", | ||
} | ||
|
||
// arrange | ||
fakeFS := fakeFsCreator{ | ||
fs: new(afero.MemMapFs), | ||
} | ||
|
||
for name, path := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
// act | ||
err := CreateFile(&fakeFS, path) | ||
|
||
// assert | ||
assert.NoError(t, err) | ||
// fileExists, _ := fakeFS.fs.Exists(path) | ||
// assert.True(t, fileExists, "file should be created") | ||
}) | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
internal/pathspltr/pathspltr.go → internal/fspath/pathspltr.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters