Skip to content

Commit 0b25aba

Browse files
authored
Delete partially fetched packages on failure. (#2410)
* Fetch pkg into tmp dir before moving on success. * Delete directory on failed fetches. * Fix typos in comment. * Move cleaning up dir to helper. * Hardcode op.
1 parent c04e211 commit 0b25aba

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

internal/util/get/get.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ func (c Command) Run(ctx context.Context) error {
8686

8787
err = kptfileutil.WriteFile(c.Destination, kf)
8888
if err != nil {
89-
return errors.E(op, types.UniquePath(c.Destination), err)
89+
return cleanUpDirAndError(c.Destination, err)
9090
}
9191

9292
p, err := pkg.New(c.Destination)
9393
if err != nil {
94-
return errors.E(op, types.UniquePath(c.Destination), err)
94+
return cleanUpDirAndError(c.Destination, err)
9595
}
9696

9797
if err = c.fetchPackages(ctx, p); err != nil {
98-
return errors.E(op, types.UniquePath(c.Destination), err)
98+
return cleanUpDirAndError(c.Destination, err)
9999
}
100100

101101
if err := addmergecomment.Process(c.Destination); err != nil {
102-
return errors.E(op, types.UniquePath(c.Destination), err)
102+
return cleanUpDirAndError(c.Destination, err)
103103
}
104104
return nil
105105
}
@@ -183,3 +183,12 @@ func (c *Command) DefaultValues() error {
183183

184184
return nil
185185
}
186+
187+
func cleanUpDirAndError(destination string, err error) error {
188+
const op errors.Op = "get.Run"
189+
rmErr := os.RemoveAll(destination)
190+
if rmErr != nil {
191+
return errors.E(op, types.UniquePath(destination), err, rmErr)
192+
}
193+
return errors.E(op, types.UniquePath(destination), err)
194+
}

internal/util/get/get_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,12 @@ func TestCommand_Run_failInvalidRepo(t *testing.T) {
739739
if !assert.Contains(t, err.Error(), "'foo' does not appear to be a git repository") {
740740
t.FailNow()
741741
}
742+
743+
// Confirm destination directory no longer exists.
744+
_, err = os.Stat(absPath)
745+
if !assert.Error(t, err) {
746+
t.FailNow()
747+
}
742748
}
743749

744750
func TestCommand_Run_failInvalidBranch(t *testing.T) {
@@ -766,6 +772,12 @@ func TestCommand_Run_failInvalidBranch(t *testing.T) {
766772
if !assert.Contains(t, err.Error(), "exit status 128") {
767773
t.FailNow()
768774
}
775+
776+
// Confirm destination directory no longer exists.
777+
_, err = os.Stat(absPath)
778+
if !assert.Error(t, err) {
779+
t.FailNow()
780+
}
769781
}
770782

771783
func TestCommand_Run_failInvalidTag(t *testing.T) {
@@ -775,13 +787,14 @@ func TestCommand_Run_failInvalidTag(t *testing.T) {
775787
})
776788
defer clean()
777789

790+
absPath := filepath.Join(w.WorkspaceDirectory, g.RepoDirectory)
778791
err := Command{
779792
Git: &kptfilev1.Git{
780793
Repo: g.RepoDirectory,
781794
Directory: "/",
782795
Ref: "refs/tags/foo",
783796
},
784-
Destination: filepath.Join(w.WorkspaceDirectory, g.RepoDirectory),
797+
Destination: absPath,
785798
}.Run(fake.CtxWithDefaultPrinter())
786799
if !assert.Error(t, err) {
787800
t.FailNow()
@@ -792,6 +805,12 @@ func TestCommand_Run_failInvalidTag(t *testing.T) {
792805
if !assert.Contains(t, err.Error(), "exit status 128") {
793806
t.FailNow()
794807
}
808+
809+
// Confirm destination directory no longer exists.
810+
_, err = os.Stat(absPath)
811+
if !assert.Error(t, err) {
812+
t.FailNow()
813+
}
795814
}
796815

797816
func TestCommand_Run_subpackages(t *testing.T) {

0 commit comments

Comments
 (0)