Skip to content

Commit

Permalink
Refactoring and testing (#7)
Browse files Browse the repository at this point in the history
* simplified `tui` package
* refactored most internals to be more resilient and testable
* greatly expanded test coverage
* aligned naming of various types across the codebase
  • Loading branch information
jippi authored Feb 15, 2024
1 parent 28a0848 commit 4d85ee1
Show file tree
Hide file tree
Showing 136 changed files with 1,125 additions and 820 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"cSpell.words": [
"bitmask",
"Printfln",
"Upsert",
"upserter"
]
Expand Down
33 changes: 22 additions & 11 deletions cmd/disable/disable.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package disable

import (
"errors"
"fmt"

"github.com/jippi/dottie/pkg"
"github.com/jippi/dottie/pkg/cli/shared"
"github.com/jippi/dottie/pkg/render"
"github.com/jippi/dottie/pkg/tui"
"github.com/spf13/cobra"
)

Expand All @@ -15,13 +15,10 @@ func NewCommand() *cobra.Command {
Use: "disable KEY",
Short: "Disable (comment out) a KEY if it exists",
GroupID: "manipulate",
Args: cobra.ExactArgs(1),
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.ExcludeDisabledAssignments).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing required argument: KEY")
}

key := args[0]
key := cmd.Flags().Arg(0)

filename := cmd.Flag("file").Value.String()

Expand All @@ -30,14 +27,28 @@ func NewCommand() *cobra.Command {
return err
}

existing := env.Get(key)
if existing == nil {
return fmt.Errorf("Could not find KEY [%s]", key)
assignment := env.Get(key)
if assignment == nil {
return fmt.Errorf("Could not find KEY [ %s ]", key)
}

if !assignment.Enabled {
tui.MaybePrintWarnings(cmd.Context(), fmt.Errorf("The key [ %s ] is already disabled", key))

return nil
}

assignment.Disable()

if err := pkg.Save(cmd.Context(), filename, env); err != nil {
return fmt.Errorf("could not save file: %w", err)
}

existing.Disable()
tui.StdoutFromContext(cmd.Context()).
Success().
Printfln("Key [ %s ] was successfully disabled", key)

return pkg.Save(filename, env)
return nil
},
}
}
4 changes: 2 additions & 2 deletions cmd/disable/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/jippi/dottie/pkg/test_helpers"
)

func TestCommand(t *testing.T) {
func TestDisableCommand(t *testing.T) {
t.Parallel()

test_helpers.RunFilebasedCommandTests(t, 0, "disable")
test_helpers.RunFileBasedCommandTests(t, 0, "disable")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [disable KEY_B]
WARNING: The key [ KEY_B ] is already disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [disable KEY_B]
(no output to stdout)
1 change: 1 addition & 0 deletions cmd/disable/tests/disable-a-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---- exec command line 0: [disable KEY_B]
2 changes: 2 additions & 0 deletions cmd/disable/tests/disable-a-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [disable KEY_B]
Key [ KEY_B ] was successfully disabled
6 changes: 6 additions & 0 deletions cmd/disable/tests/disable-key-b.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions cmd/disable/tests/disable-key-b/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [disable KEY_B]
(no output to stderr)
2 changes: 2 additions & 0 deletions cmd/disable/tests/disable-key-b/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [disable KEY_B]
Key [ KEY_B ] was successfully disabled
File renamed without changes.
4 changes: 3 additions & 1 deletion cmd/disable/tests/invalid-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Could not find KEY [NONEXISTING_KEY]
---- exec command line 0: [disable NONEXISTING_KEY]
Error: Could not find KEY [ NONEXISTING_KEY ]
Run 'dottie disable --help' for usage.
2 changes: 2 additions & 0 deletions cmd/disable/tests/invalid-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [disable NONEXISTING_KEY]
(no output to stdout)
File renamed without changes.
4 changes: 3 additions & 1 deletion cmd/disable/tests/missing-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Missing required argument: KEY
---- exec command line 0: [disable]
Error: accepts 1 arg(s), received 0
Run 'dottie disable --help' for usage.
2 changes: 2 additions & 0 deletions cmd/disable/tests/missing-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [disable]
(no output to stdout)
33 changes: 21 additions & 12 deletions cmd/enable/enable.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
package enable

import (
"errors"
"fmt"

"github.com/jippi/dottie/pkg"
"github.com/jippi/dottie/pkg/cli/shared"
"github.com/jippi/dottie/pkg/render"
"github.com/jippi/dottie/pkg/tui"
"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "enable KEY",
Short: "Enable (uncomment) a KEY if it exists",
Args: cobra.ExactArgs(1),
GroupID: "manipulate",
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.ExcludeActiveAssignments).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing required argument: KEY")
}

filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
document, err := pkg.Load(filename)
if err != nil {
return err
}

key := args[0]
key := cmd.Flags().Arg(0)

assignment := document.Get(key)
if assignment == nil {
return fmt.Errorf("Could not find KEY [ %s ]", key)
}

if assignment.Enabled {
tui.MaybePrintWarnings(cmd.Context(), fmt.Errorf("The key [ %s ] is already enabled", key))
}

assignment.Enable()

existing := env.Get(key)
if existing == nil {
return fmt.Errorf("Could not find KEY [%s]", key)
if err := pkg.Save(cmd.Context(), filename, document); err != nil {
return fmt.Errorf("could not save file: %w", err)
}

existing.Enable()
tui.StdoutFromContext(cmd.Context()).
Success().
Printfln("Key [ %s ] was successfully enabled", key)

return pkg.Save(filename, env)
return nil
},
}
}
4 changes: 2 additions & 2 deletions cmd/enable/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/jippi/dottie/pkg/test_helpers"
)

func TestCommand(t *testing.T) {
func TestEnableCommand(t *testing.T) {
t.Parallel()

test_helpers.RunFilebasedCommandTests(t, 0, "enable")
test_helpers.RunFileBasedCommandTests(t, 0, "enable")
}
2 changes: 2 additions & 0 deletions cmd/enable/tests/enable-a-key-already-enabled/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [enable KEY_B]
WARNING: The key [ KEY_B ] is already enabled
2 changes: 2 additions & 0 deletions cmd/enable/tests/enable-a-key-already-enabled/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [enable KEY_B]
Key [ KEY_B ] was successfully enabled
File renamed without changes.
2 changes: 2 additions & 0 deletions cmd/enable/tests/enable-a-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [enable KEY_B]
(no output to stderr)
2 changes: 2 additions & 0 deletions cmd/enable/tests/enable-a-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [enable KEY_B]
Key [ KEY_B ] was successfully enabled
File renamed without changes.
4 changes: 3 additions & 1 deletion cmd/enable/tests/invalid-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Could not find KEY [NONEXISTING_KEY]
---- exec command line 0: [enable NONEXISTING_KEY]
Error: Could not find KEY [ NONEXISTING_KEY ]
Run 'dottie enable --help' for usage.
2 changes: 2 additions & 0 deletions cmd/enable/tests/invalid-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [enable NONEXISTING_KEY]
(no output to stdout)
File renamed without changes.
4 changes: 3 additions & 1 deletion cmd/enable/tests/missing-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Missing required argument: KEY
---- exec command line 0: [enable]
Error: accepts 1 arg(s), received 0
Run 'dottie enable --help' for usage.
2 changes: 2 additions & 0 deletions cmd/enable/tests/missing-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [enable]
(no output to stdout)
9 changes: 6 additions & 3 deletions cmd/fmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "fmt",
Short: "Format a .env file",
Args: cobra.NoArgs,
GroupID: "manipulate",
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
document, err := pkg.Load(filename)
if err != nil {
return err
}

if err := pkg.Save(filename, env); err != nil {
if err := pkg.Save(cmd.Context(), filename, document); err != nil {
return err
}

tui.Theme.Success.StdoutPrinter().Printfln("File [%s] was successfully formatted", filename)
tui.StdoutFromContext(cmd.Context()).
Success().
Printfln("File [ %s ] was successfully formatted", filename)

return nil
},
Expand Down
22 changes: 12 additions & 10 deletions cmd/groups/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,33 @@ func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "groups",
Short: "Print groups found in the .env file",
Args: cobra.NoArgs,
GroupID: "output",
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
document, err := pkg.Load(filename)
if err != nil {
return err
}

groups := env.Groups
groups := document.Groups
if len(groups) == 0 {
return errors.New("No groups found")
}

width := longesGroupName(groups)
maxWidth := longesGroupName(groups)

light := tui.Theme.Secondary.BuffPrinter(cmd.OutOrStdout())
key := tui.Theme.Primary.BuffPrinter(cmd.OutOrStdout())
info := tui.Theme.Info.BuffPrinter(cmd.OutOrStdout())
info.Box("Groups in " + filename)
stdout := tui.StdoutFromContext(cmd.Context())
primary := stdout.Primary()
secondary := stdout.Secondary()

stdout.Info().Box("Groups in " + filename)

for _, group := range groups {
key.Printf("%-"+strconv.Itoa(width)+"s", slug.Make(group.String()))
key.Print(" ")
light.Printfln("(%s:%d)", filename, group.Position.FirstLine)
primary.Printf("%-"+strconv.Itoa(maxWidth)+"s", slug.Make(group.String()))
primary.Print(" ")
secondary.Printfln("(%s:%d)", filename, group.Position.FirstLine)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/groups/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/jippi/dottie/pkg/test_helpers"
)

func TestCommand(t *testing.T) {
func TestGroupsCommand(t *testing.T) {
t.Parallel()

test_helpers.RunFilebasedCommandTests(t, test_helpers.SkipEnvCopy, "groups")
test_helpers.RunFileBasedCommandTests(t, test_helpers.ReadOnly, "groups")
}
File renamed without changes.
2 changes: 2 additions & 0 deletions cmd/groups/tests/multiple-groups/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [groups]
(no output to stderr)
11 changes: 6 additions & 5 deletions cmd/groups/tests/multiple-groups/stdout.golden
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ │
│ Groups in tests/multiple-groups.env │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
---- exec command line 0: [groups]
┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ Groups in tests/multiple-groups.env │
│ │
└──────────────────────────────────────────────────────────────────────────────┘
my-first-group (tests/multiple-groups.env:2)
my-second-group (tests/multiple-groups.env:13)
my-third-group (tests/multiple-groups.env:17)
File renamed without changes.
4 changes: 3 additions & 1 deletion cmd/groups/tests/no-groups/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
No groups found
---- exec command line 0: [groups]
Error: No groups found
Run 'dottie groups --help' for usage.
2 changes: 2 additions & 0 deletions cmd/groups/tests/no-groups/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [groups]
(no output to stdout)
File renamed without changes.
2 changes: 2 additions & 0 deletions cmd/groups/tests/single-group/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- exec command line 0: [groups]
(no output to stderr)
11 changes: 6 additions & 5 deletions cmd/groups/tests/single-group/stdout.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ │
│ Groups in tests/single-group.env │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
---- exec command line 0: [groups]
┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ Groups in tests/single-group.env │
│ │
└──────────────────────────────────────────────────────────────────────────────┘
my-group (tests/single-group.env:2)
7 changes: 4 additions & 3 deletions cmd/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "json",
Short: "Print as JSON",
Args: cobra.NoArgs,
GroupID: "output",
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
document, err := pkg.Load(filename)
if err != nil {
return err
}

b, err := json.MarshalIndent(env, "", " ")
output, err := json.MarshalIndent(document, "", " ")
if err != nil {
return err
}

fmt.Println(string(b))
fmt.Println(string(output))

return nil
},
Expand Down
13 changes: 0 additions & 13 deletions cmd/print/prin_test.go

This file was deleted.

Loading

0 comments on commit 4d85ee1

Please sign in to comment.