Skip to content

Commit

Permalink
test: add initial unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isometry committed Sep 23, 2024
1 parent f402afc commit c2d1372
Show file tree
Hide file tree
Showing 6 changed files with 711 additions and 37 deletions.
73 changes: 36 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,34 @@ An arbitrary number of content adds, removes and deletes can be committed withou

```console
$ ghup content --help

Manage content via the GitHub V4 API

Usage:
ghup content [flags] [<file-spec> ...]

Flags:
--base-branch string base branch name (default: "[remote-default-branch]")
--create-branch create missing target branch (default true)
-d, --delete strings file-path to delete
-h, --help help for content
--pr-body string pull request description body
--pr-draft create pull request in draft mode
--pr-title string create pull request iff target branch is created and title is specified
-s, --separator string file-spec separator (default ":")
-u, --update strings file-spec to update
--create-branch create missing target branch (default true)
--pr-title string create pull request iff target branch is created and title is specified
--pr-body string pull request body
--pr-draft create pull request in draft mode
--base-branch name base branch name (default: "[remote-default-branch])"
-s, --separator string file-spec separator (default ":")
-u, --update file-spec file-spec to update
-d, --delete file-path file-path to delete
-h, --help help for content

Global Flags:
--author.trailer string key for commit author trailer (blank to disable) (default "Co-Authored-By")
-b, --branch string target branch name (default "feature/ref")
-f, --force force action
-m, --message string message (default "Commit via API")
-o, --owner string repository owner (default "isometry")
-r, --repo string repository name (default "ghup")
--token string GitHub Token or path/to/token-file
--trailer stringToString additional commit trailer (key=value; JSON via environment) (default [])
--user.email string email for commit author trailer (default "[email protected]")
--user.name string name for commit author trailer (default "Robin Breathe")
-v, --verbosity count verbosity
--author.trailer key key for commit author trailer (blank to disable) (default "Co-Authored-By")
-b, --branch name target branch name (default "[local-branch-or-main]")
-f, --force force action
-m, --message string message (default "Commit via API")
-o, --owner name repository owner name (default "[owner-of-first-github-remote-or-required]")
-r, --repo name repository name (default "[repo-of-first-github-remote-or-required]")
--token string GitHub Token or path/to/token-file
--trailer key=value extra key=value commit trailers (default [])
--user.email email email for commit author trailer (default "[user.email]")
--user.name name name for commit author trailer (default "[user.name]")
-v, --verbosity count verbosity
```

Each `file-spec` provided as a positional argument or explicitly via the `--update` flag takes the form `<local-file-path>[:<remote-target-path>]`. Content is read from the local file `<local-file-path>` and written to `<remote-target-path>` (defaulting to `<local-file-path>` if not specified).
Expand Down Expand Up @@ -124,17 +123,17 @@ Flags:
--tag string tag name

Global Flags:
--author.trailer string key for commit author trailer (blank to disable) (default "Co-Authored-By")
-b, --branch string target branch name (default "feature/ref")
-f, --force force action
-m, --message string message (default "Commit via API")
-o, --owner string repository owner (default "isometry")
-r, --repo string repository name (default "ghup")
--token string GitHub Token or path/to/token-file
--trailer stringToString additional commit trailer (key=value; JSON via environment) (default [])
--user.email string email for commit author trailer (default "[email protected]")
--user.name string name for commit author trailer (default "Robin Breathe")
-v, --verbosity count verbosity
--author.trailer key key for commit author trailer (blank to disable) (default "Co-Authored-By")
-b, --branch name target branch name (default "[local-branch-or-main]")
-f, --force force action
-m, --message string message (default "Commit via API")
-o, --owner name repository owner name (default "[owner-of-first-github-remote-or-required]")
-r, --repo name repository name (default "[repo-of-first-github-remote-or-required]")
--token string GitHub Token or path/to/token-file
--trailer key=value extra key=value commit trailers (default [])
--user.email email email for commit author trailer (default "[user.email]")
--user.name name name for commit author trailer (default "[user.name]")
-v, --verbosity count verbosity
```

#### Tagging Examples
Expand Down Expand Up @@ -180,15 +179,15 @@ Flags:

Global Flags:
--author.trailer key key for commit author trailer (blank to disable) (default "Co-Authored-By")
-b, --branch name target branch name (default "feature/ref")
-b, --branch name target branch name (default "[local-branch-or-main]")
-f, --force force action
-m, --message string message (default "Commit via API")
-o, --owner name repository owner name (default "isometry")
-r, --repo name repository name (default "ghup")
-o, --owner name repository owner name (default "[owner-of-first-github-remote-or-required]")
-r, --repo name repository name (default "[repo-of-first-github-remote-or-required]")
--token string GitHub Token or path/to/token-file
--trailer key=value extra key=value commit trailers (default [])
--user.email email email for commit author trailer (default "[email protected]")
--user.name name name for commit author trailer (default "Robin Breathe")
--user.email email email for commit author trailer (default "[user.email]")
--user.name name name for commit author trailer (default "[user.name]")
-v, --verbosity count verbosity
```

Expand Down
1 change: 1 addition & 0 deletions internal/local/testdata/testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test content
77 changes: 77 additions & 0 deletions internal/local/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package local

import (
"bytes"
"path/filepath"
"strings"
"testing"
)

func TestGetLocalFileContent(t *testing.T) {
testFilePath := filepath.Join("testdata", "testfile.txt")
testFileContent := []byte("test content\n")
tests := []struct {
name string
arg string
separator string
wantTarget string
wantContent []byte
wantErr bool
}{
{
name: "Single file",
arg: testFilePath,
separator: ":",
wantTarget: testFilePath,
wantContent: testFileContent,
},
{
name: "Source and target",
arg: strings.Join([]string{testFilePath, "destfile.txt"}, ":"),
separator: ":",
wantTarget: "destfile.txt",
wantContent: testFileContent,
},
{
name: "Empty parameter",
arg: "",
separator: ":",
wantErr: true,
},
{
name: "Missing source",
arg: ":destfile.txt",
separator: ":",
wantErr: true,
},
{
name: "Missing target",
arg: strings.Join([]string{testFilePath, ""}, ":"),
separator: ":",
wantErr: true,
},
{
name: "Alternate separator",
arg: strings.Join([]string{testFilePath, "destfile.txt"}, "=>"),
separator: "=>",
wantTarget: "destfile.txt",
wantContent: testFileContent,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotTarget, gotContent, err := GetLocalFileContent(tt.arg, tt.separator)
if (err != nil) != tt.wantErr {
t.Errorf("GetLocalFileContent() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotTarget != tt.wantTarget {
t.Errorf("GetLocalFileContent() gotTarget = %v, want %v", gotTarget, tt.wantTarget)
}
if !bytes.Equal(gotContent, tt.wantContent) {
t.Errorf("GetLocalFileContent() gotContent = %v, want %v", gotContent, tt.wantContent)
}
})
}
}
48 changes: 48 additions & 0 deletions internal/remote/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package remote

import (
"reflect"
"testing"

"github.com/shurcooL/githubv4"
)

func TestCommitMessage(t *testing.T) {
tests := []struct {
name string
message string
expected githubv4.CommitMessage
}{
{
name: "Empty message",
message: "",
expected: githubv4.CommitMessage{
Headline: githubv4.String(""),
},
},
{
name: "Single line message",
message: "This is a headline",
expected: githubv4.CommitMessage{
Headline: githubv4.String("This is a headline"),
},
},
{
name: "Multi-line message",
message: "This is a headline\nThis is the body",
expected: githubv4.CommitMessage{
Headline: githubv4.String("This is a headline"),
Body: githubv4.NewString(githubv4.String("This is the body")),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := CommitMessage(tt.message)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("CommitMessage(%v) = %v; expected %v", tt.message, result, tt.expected)
}
})
}
}
Loading

0 comments on commit c2d1372

Please sign in to comment.