From c0eb729ce0bfe415b15c679305db5780b8a21c97 Mon Sep 17 00:00:00 2001 From: Dinesh Date: Sat, 5 Jan 2019 13:20:29 +0530 Subject: [PATCH] Create ~/.tmuxp dir if not present --- pkg/filesystem/filesystem.go | 9 +++++++++ pkg/internal/mock/filesystem.go | 12 ++++++++++++ pkg/tmuxp/tmuxp.go | 4 ++++ pkg/tmuxp/tmuxp_test.go | 16 ++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/pkg/filesystem/filesystem.go b/pkg/filesystem/filesystem.go index 9361d0c..e639f43 100644 --- a/pkg/filesystem/filesystem.go +++ b/pkg/filesystem/filesystem.go @@ -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 @@ -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 +} diff --git a/pkg/internal/mock/filesystem.go b/pkg/internal/mock/filesystem.go index 1f02941..7b178fc 100644 --- a/pkg/internal/mock/filesystem.go +++ b/pkg/internal/mock/filesystem.go @@ -83,3 +83,15 @@ func (m *FileSystem) Create(file string) (io.Writer, error) { func (mr *FileSystemMockRecorder) Create(file interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*FileSystem)(nil).Create), file) } + +// CreateDirIfNotExist mocks base method +func (m *FileSystem) CreateDirIfNotExist(dir string) error { + ret := m.ctrl.Call(m, "CreateDirIfNotExist", dir) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateDirIfNotExist indicates an expected call of CreateDirIfNotExist +func (mr *FileSystemMockRecorder) CreateDirIfNotExist(dir interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateDirIfNotExist", reflect.TypeOf((*FileSystem)(nil).CreateDirIfNotExist), dir) +} diff --git a/pkg/tmuxp/tmuxp.go b/pkg/tmuxp/tmuxp.go index 1058a6e..f17ffb4 100644 --- a/pkg/tmuxp/tmuxp.go +++ b/pkg/tmuxp/tmuxp.go @@ -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, diff --git a/pkg/tmuxp/tmuxp_test.go b/pkg/tmuxp/tmuxp_test.go index f2b547a..6ea3b81 100644 --- a/pkg/tmuxp/tmuxp_test.go +++ b/pkg/tmuxp/tmuxp_test.go @@ -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) @@ -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) { @@ -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()) @@ -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) @@ -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)