Skip to content

Commit

Permalink
add a simple unit test to prevent regression
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman committed Sep 7, 2023
1 parent ef1c37c commit 5023ec4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/infra/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ func putUpdaterInputs(ctx context.Context, cli *client.Client, cert, id string,
return fmt.Errorf("failed to copy cert to container: %w", err)
}

data, err := json.Marshal(FileFetcherJobFile{Job: job})
data, err := JobFile{Job: job}.ToJSON()
if err != nil {
return fmt.Errorf("failed to marshal job file: %w", err)
}
if t, err := tarball(guestInputDir, string(data)); err != nil {
if t, err := tarball(guestInputDir, data); err != nil {
return fmt.Errorf("failed create input tarball: %w", err)
} else if err = cli.CopyToContainer(ctx, id, "/", t, opt); err != nil {
return fmt.Errorf("failed to copy input to container: %w", err)
Expand Down Expand Up @@ -278,11 +278,16 @@ func (u *Updater) Close() error {
})
}

// FileFetcherJobFile is the payload passed to file updater containers.
type FileFetcherJobFile struct {
// JobFile is the payload passed to file updater containers.
type JobFile struct {
Job *model.Job `json:"job"`
}

func (j JobFile) ToJSON() (string, error) {
data, err := json.Marshal(j)
return string(data), err
}

func tarball(name, contents string) (*bytes.Buffer, error) {
var buf bytes.Buffer
t := tar.NewWriter(&buf)
Expand Down
21 changes: 21 additions & 0 deletions internal/infra/updater_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package infra

import (
"github.com/dependabot/cli/internal/model"
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -48,3 +50,22 @@ func Test_mountOptions(t *testing.T) {
}
}
}

func TestJobFile_ToJSON(t *testing.T) {
t.Run("empty commit doesn't pass in empty string", func(t *testing.T) {
job := JobFile{
Job: &model.Job{
Source: model.Source{
Commit: "",
},
},
}
json, err := job.ToJSON()
if err != nil {
t.Fatal(err)
}
if strings.Contains(json, `"commit"`) {
t.Errorf("expected JSON to not contain commit: %v", json)
}
})
}

0 comments on commit 5023ec4

Please sign in to comment.