Skip to content

Commit

Permalink
atlasexec: use value receiver to keep original atlas client (#74)
Browse files Browse the repository at this point in the history
* atlasexec: use value receiver to keep original atlas client

* atlasexec: add unit test

* atlasexec: change workingDir property instead of creating new client

* atlasexec: typo
  • Loading branch information
luantranminh authored Jun 17, 2024
1 parent 321ed4f commit 0e0db50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func NewClient(workingDir, execPath string) (_ *Client, err error) {
// return err
// })
func (c *Client) WithWorkDir(dir string, fn func(*Client) error) error {
wd := c.workingDir
defer func() { c.workingDir = wd }()
c.workingDir = dir
return fn(c)
}
Expand Down
12 changes: 12 additions & 0 deletions atlasexec/working_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ func TestContextExecer(t *testing.T) {
require.Equal(t, "atlas.hcl\nmigrations\n", buf.String())
require.NoError(t, ce.Close())
}

func TestMaintainOriginalWorkingDir(t *testing.T) {
dir := t.TempDir()
c, err := NewClient(dir, "atlas")
require.NoError(t, err)
require.Equal(t, dir, c.workingDir)
require.NoError(t, c.WithWorkDir("bar", func(c *Client) error {
require.Equal(t, "bar", c.workingDir)
return nil
}))
require.Equal(t, dir, c.workingDir, "The working directory should not be changed")
}

0 comments on commit 0e0db50

Please sign in to comment.