Skip to content

Commit

Permalink
Merge pull request #8 from dineshba/create-dot-tmuxp-dir-if-not-present
Browse files Browse the repository at this point in the history
Create ~/.tmuxp dir if not present
  • Loading branch information
arunvelsriram authored Feb 12, 2019
2 parents efb0e81 + c0eb729 commit 4ef003a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type FileSystem interface {
HomeDir() (string, error)
Open(file string) (io.Reader, error)
Create(file string) (io.Writer, error)
CreateDirIfNotExist(dir string) error
}

// Default represents the Operating System's filesystem
Expand Down Expand Up @@ -55,3 +56,11 @@ func (d *Default) Create(file string) (io.Writer, error) {

return writer, nil
}

// CreateDirIfNotExist creates a new directory if it already exists
func (d *Default) CreateDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return os.Mkdir(dir, 0755)
}
return nil
}
12 changes: 12 additions & 0 deletions pkg/internal/mock/filesystem.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/tmuxp/tmuxp.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func NewConfig(sessionName string, windows Windows, environment Environment, fs
}
tmuxpCfgsDir := path.Join(home, ".tmuxp")

err = fs.CreateDirIfNotExist(tmuxpCfgsDir)
if err != nil {
return nil, err
}
return &Config{
SessionName: sessionName,
Windows: windows,
Expand Down
16 changes: 16 additions & 0 deletions pkg/tmuxp/tmuxp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestNewConfig(t *testing.T) {

mockFS := mock.NewFileSystem(ctrl)
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
mockFS.EXPECT().CreateDirIfNotExist(gomock.Eq("/Users/test/.tmuxp")).Return(nil)
tmuxpCfg, err := tmuxp.NewConfig("session", tmuxp.Windows{}, tmuxp.Environment{}, mockFS)

assert.Nil(t, err)
Expand All @@ -34,6 +35,18 @@ func TestNewConfig(t *testing.T) {

assert.EqualError(t, err, "some error")
})

t.Run("should return error in create .tmuxp dir is failed", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockFS := mock.NewFileSystem(ctrl)
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
mockFS.EXPECT().CreateDirIfNotExist(gomock.Eq("/Users/test/.tmuxp")).Return(fmt.Errorf("error creating .tmuxp dir"))
_, err := tmuxp.NewConfig("session", tmuxp.Windows{}, tmuxp.Environment{}, mockFS)

assert.EqualError(t, err, "error creating .tmuxp dir")
})
}

func TestTmuxpConfigsDir(t *testing.T) {
Expand All @@ -42,6 +55,7 @@ func TestTmuxpConfigsDir(t *testing.T) {

mockFS := mock.NewFileSystem(ctrl)
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
mockFS.EXPECT().CreateDirIfNotExist(gomock.Eq("/Users/test/.tmuxp")).Return(nil)
tmuxpCfg, _ := tmuxp.NewConfig("session", tmuxp.Windows{}, tmuxp.Environment{}, mockFS)

assert.Equal(t, "/Users/test/.tmuxp", tmuxpCfg.TmuxpConfigsDir())
Expand All @@ -55,6 +69,7 @@ func TestSave(t *testing.T) {
mockFS := mock.NewFileSystem(ctrl)
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
var writer bytes.Buffer
mockFS.EXPECT().CreateDirIfNotExist(gomock.Eq("/Users/test/.tmuxp")).Return(nil)
mockFS.EXPECT().Create("tmuxp-config.yaml").Return(&writer, nil)
tmuxpCfg, _ := tmuxp.NewConfig("session", tmuxp.Windows{{Name: "window"}}, tmuxp.Environment{"TEST_ENV": "value", "ANOTHER_TEST_ENV": "another-value"}, mockFS)

Expand All @@ -79,6 +94,7 @@ environment:

mockFS := mock.NewFileSystem(ctrl)
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
mockFS.EXPECT().CreateDirIfNotExist(gomock.Eq("/Users/test/.tmuxp")).Return(nil)
mockFS.EXPECT().Create("tmuxp-config.yaml").Return(nil, fmt.Errorf("some error"))
tmuxpCfg, _ := tmuxp.NewConfig("session", tmuxp.Windows{{Name: "window"}}, tmuxp.Environment{"TEST_ENV": "value", "ANOTHER_TEST_ENV": "another-value"}, mockFS)

Expand Down

0 comments on commit 4ef003a

Please sign in to comment.