Skip to content

Commit

Permalink
fix(cli): PEBBLE_COPY_ONCE on missing dir. (canonical#392)
Browse files Browse the repository at this point in the history
If the `PEBBLE_COPY_ONCE` source directory does not exist, it will fail
to copy inside the borrowed std go copyFS function. This adds an
`os.Stat` to check if it first exists, ignoring it otherwise (the
expected behavior).

---------

Co-authored-by: Ben Hoyt <[email protected]>
  • Loading branch information
hpidcock and benhoyt committed Mar 21, 2024
1 parent db6249d commit a847e85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internals/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ func maybeCopyPebbleDir(destDir, srcDir string) error {
if srcDir == "" {
return nil
}
_, err := os.Stat(srcDir)
if errors.Is(err, os.ErrNotExist) {
// Skip missing source directory.
return nil
} else if err != nil {
return err
}
entries, err := os.ReadDir(destDir)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
Expand Down
22 changes: 22 additions & 0 deletions internals/cli/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,25 @@ func (s *PebbleSuite) TestMaybeCopyPebbleDirNoCopy(c *C) {
"a.yaml": true,
})
}

func (s *PebbleSuite) TestMaybeCopyPebbleDirSourceNotExist(c *C) {
tmpDir := c.MkDir()
dst := path.Join(tmpDir, "dst")
err := os.Mkdir(dst, 0o700)
c.Assert(err, IsNil)
src := path.Join(tmpDir, "not-exist")
err = cli.MaybeCopyPebbleDir(dst, src)
c.Assert(err, IsNil)
}

func (s *PebbleSuite) TestMaybeCopyPebbleDirSourceNotADirectory(c *C) {
tmpDir := c.MkDir()
dst := path.Join(tmpDir, "dst")
err := os.Mkdir(dst, 0o700)
c.Assert(err, IsNil)
src := path.Join(tmpDir, "file")
err = os.WriteFile(src, nil, 0o666)
c.Assert(err, IsNil)
err = cli.MaybeCopyPebbleDir(dst, src)
c.Assert(err, ErrorMatches, ".*not a directory.*")
}

0 comments on commit a847e85

Please sign in to comment.