Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring and testing #7

Merged
merged 33 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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)

Check warning on line 44 in cmd/disable/disable.go

View check run for this annotation

Codecov / codecov/patch

cmd/disable/disable.go#L44

Added line #L44 was not covered by tests
}

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"
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
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)
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)

Check warning on line 42 in cmd/enable/enable.go

View check run for this annotation

Codecov / codecov/patch

cmd/enable/enable.go#L42

Added line #L42 was not covered by tests
}

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
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
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)
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 @@
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)

Check warning on line 18 in cmd/fmt/fmt.go

View check run for this annotation

Codecov / codecov/patch

cmd/fmt/fmt.go#L18

Added line #L18 was not covered by tests
if err != nil {
return err
}

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

Check warning on line 23 in cmd/fmt/fmt.go

View check run for this annotation

Codecov / codecov/patch

cmd/fmt/fmt.go#L23

Added line #L23 was not covered by tests
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)

Check warning on line 29 in cmd/fmt/fmt.go

View check run for this annotation

Codecov / codecov/patch

cmd/fmt/fmt.go#L27-L29

Added lines #L27 - L29 were not covered by tests

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")
}
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)
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 @@
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)

Check warning on line 20 in cmd/json/json.go

View check run for this annotation

Codecov / codecov/patch

cmd/json/json.go#L20

Added line #L20 was not covered by tests
if err != nil {
return err
}

b, err := json.MarshalIndent(env, "", " ")
output, err := json.MarshalIndent(document, "", " ")

Check warning on line 25 in cmd/json/json.go

View check run for this annotation

Codecov / codecov/patch

cmd/json/json.go#L25

Added line #L25 was not covered by tests
if err != nil {
return err
}

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

Check warning on line 30 in cmd/json/json.go

View check run for this annotation

Codecov / codecov/patch

cmd/json/json.go#L30

Added line #L30 was not covered by tests

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

This file was deleted.

Loading
Loading