Skip to content

Commit

Permalink
Rename commands: set + unset
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed Jun 2, 2023
1 parent 9df38f8 commit 736b8fc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var message = template.Must(
template.New("message").Parse("Subject (keep under 50 characters)\n\nContext/description (what and why){{if .Issue}}\n\nAddresses: {{.Issue}}{{end}}{{if .Pair}}\n\nCo-authored-by: {{.Pair}}{{end}}"),
)

var TemplateCommand = &cli.Command{
Name: "template",
var SetTemplateCommand = &cli.Command{
Name: "set",
Usage: "Set up template for commit message",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "dry-run", Aliases: []string{"d"}, Usage: "Print template to stdout", Value: false},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/urfave/cli/v2"
)

func setupTemplateTest() func() {
func setupSetTemplateTest() func() {
currentDir, _ := os.Getwd()
dir, err := os.MkdirTemp(os.TempDir(), "test")
if err != nil {
Expand All @@ -28,14 +28,14 @@ func setupTemplateTest() func() {
}

func TestCreateTemplateFile(t *testing.T) {
teardown := setupTemplateTest()
teardown := setupSetTemplateTest()
defer teardown()

app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{TemplateCommand}}
app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{SetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(app, set, nil)

_ = TemplateCommand.Run(c, []string{"template"}...)
_ = SetTemplateCommand.Run(c, []string{"set"}...)

_, err := os.Stat(".git/.gitmessage.txt")
if err != nil {
Expand All @@ -44,14 +44,14 @@ func TestCreateTemplateFile(t *testing.T) {
}

func TestWriteMessageToTemplateFile(t *testing.T) {
teardown := setupTemplateTest()
teardown := setupSetTemplateTest()
defer teardown()

app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{TemplateCommand}}
app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{SetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(app, set, nil)

_ = TemplateCommand.Run(c, []string{"template"}...)
_ = SetTemplateCommand.Run(c, []string{"set"}...)

content, err := os.ReadFile(".git/.gitmessage.txt")
if err != nil {
Expand All @@ -65,14 +65,14 @@ func TestWriteMessageToTemplateFile(t *testing.T) {
}

func TestSetCommitTemplateGitConfig(t *testing.T) {
teardown := setupTemplateTest()
teardown := setupSetTemplateTest()
defer teardown()

app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{TemplateCommand}}
app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{SetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(app, set, nil)

_ = TemplateCommand.Run(c, []string{"template"}...)
_ = SetTemplateCommand.Run(c, []string{"set"}...)

config, err := exec.Command("git", "config", "--local", "commit.template").Output()
if err != nil {
Expand All @@ -86,30 +86,30 @@ func TestSetCommitTemplateGitConfig(t *testing.T) {
}
}

func ExampleTemplateCommand_dryRunBasic() {
app := &cli.App{Writer: os.Stdout, Commands: []*cli.Command{TemplateCommand}}
func ExampleSetTemplateCommand_dryRunBasic() {
app := &cli.App{Writer: os.Stdout, Commands: []*cli.Command{SetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
set.Bool("dry-run", true, "")
_ = set.Parse([]string{"--dry-run"})
c := cli.NewContext(app, set, nil)

_ = TemplateCommand.Run(c, []string{"template", "--dry-run"}...)
_ = SetTemplateCommand.Run(c, []string{"set", "--dry-run"}...)

// Output:
// Subject (keep under 50 characters)
//
// Context/description (what and why)
}

func ExampleTemplateCommand_dryRunWithIssueRef() {
app := &cli.App{Writer: os.Stdout, Commands: []*cli.Command{TemplateCommand}}
func ExampleSetTemplateCommand_dryRunWithIssueRef() {
app := &cli.App{Writer: os.Stdout, Commands: []*cli.Command{SetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
set.Bool("dry-run", true, "")
set.String("issue-ref", "#123", "")
_ = set.Parse([]string{"--dry-run", "--issue-ref", "#123"})
c := cli.NewContext(app, set, nil)

_ = TemplateCommand.Run(c, []string{"template", "--dry-run", "--issue-ref", "#123"}...)
_ = SetTemplateCommand.Run(c, []string{"set", "--dry-run", "--issue-ref", "#123"}...)

// Output:
// Subject (keep under 50 characters)
Expand All @@ -119,15 +119,15 @@ func ExampleTemplateCommand_dryRunWithIssueRef() {
// Addresses: #123
}

func ExampleTemplateCommand_dryRunWithCoAuthor() {
app := &cli.App{Writer: os.Stdout, Commands: []*cli.Command{TemplateCommand}}
func ExampleSetTemplateCommand_dryRunWithCoAuthor() {
app := &cli.App{Writer: os.Stdout, Commands: []*cli.Command{SetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
set.Bool("dry-run", true, "")
set.String("pair", "John Doe <[email protected]>", "")
_ = set.Parse([]string{"--dry-run", "--pair", "John Doe <[email protected]>"})
c := cli.NewContext(app, set, nil)

_ = TemplateCommand.Run(c, []string{"template", "--dry-run", "--pair", "John Doe <[email protected]>"}...)
_ = SetTemplateCommand.Run(c, []string{"set", "--dry-run", "--pair", "John Doe <[email protected]>"}...)

// Output:
// Subject (keep under 50 characters)
Expand All @@ -137,16 +137,16 @@ func ExampleTemplateCommand_dryRunWithCoAuthor() {
// Co-authored-by: John Doe <[email protected]>
}

func ExampleTemplateCommand_dryRunWithAll() {
app := &cli.App{Writer: os.Stdout, Commands: []*cli.Command{TemplateCommand}}
func ExampleSetTemplateCommand_dryRunWithAll() {
app := &cli.App{Writer: os.Stdout, Commands: []*cli.Command{SetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
set.Bool("dry-run", true, "")
set.String("issue-ref", "#123", "")
set.String("pair", "John Doe <[email protected]>", "")
_ = set.Parse([]string{"--dry-run", "--issue-ref", "#123", "--pair", "John Doe <[email protected]>"})
c := cli.NewContext(app, set, nil)

_ = TemplateCommand.Run(c, []string{"template", "--dry-run", "--issue-ref", "#123", "--pair", "John Doe <[email protected]>"}...)
_ = SetTemplateCommand.Run(c, []string{"set", "--dry-run", "--issue-ref", "#123", "--pair", "John Doe <[email protected]>"}...)

// Output:
// Subject (keep under 50 characters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/urfave/cli/v2"
)

var ClearCommand = &cli.Command{
Name: "clear",
var UnsetTemplateCommand = &cli.Command{
Name: "unset",
Usage: "Unset current template",
Action: func(c *cli.Context) error {
// Ignore error here, file may have been removed manually...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func TestRemoveTemplateFile(t *testing.T) {
panic(err)
}

app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{ClearCommand}}
app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{UnsetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(app, set, nil)

_ = ClearCommand.Run(c, []string{"clear"}...)
_ = UnsetTemplateCommand.Run(c, []string{"unset"}...)

// If the respective file is no longer present, os.Stat returns an error, thus we must expect an error here
// if the file has been correctly removed!
Expand All @@ -62,11 +62,11 @@ func TestUnsetCommitTemplateGitConfig(t *testing.T) {
panic(err)
}

app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{ClearCommand}}
app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{UnsetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(app, set, nil)

_ = ClearCommand.Run(c, []string{"clear"}...)
_ = UnsetTemplateCommand.Run(c, []string{"unset"}...)

// If the respective config is not set, git exits with 1, thus we must expect an error here
// if the config has been correctly removed!
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: adapt help output
package main

import (
Expand All @@ -14,8 +13,8 @@ func main() {
Name: "git-commit-template",
Usage: "Set up a useful template for commit messages",
Commands: []*cli.Command{
command.TemplateCommand,
command.ClearCommand,
command.SetTemplateCommand,
command.UnsetTemplateCommand,
},
}

Expand Down

0 comments on commit 736b8fc

Please sign in to comment.