Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Feb 15, 2024
1 parent 6951a78 commit 25a2079
Show file tree
Hide file tree
Showing 39 changed files with 123 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/disable/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [disable KEY_B]
(no output to stdout)
1 change: 1 addition & 0 deletions cmd/disable/tests/disable-key-b/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [disable KEY_B]
(no output to stderr)
1 change: 1 addition & 0 deletions cmd/disable/tests/invalid-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [disable NONEXISTING_KEY]
(no output to stdout)
1 change: 1 addition & 0 deletions cmd/disable/tests/missing-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [disable]
(no output to stdout)
2 changes: 1 addition & 1 deletion cmd/enable/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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")
Expand Down
1 change: 1 addition & 0 deletions cmd/enable/tests/enable-a-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [enable KEY_B]
(no output to stderr)
1 change: 1 addition & 0 deletions cmd/enable/tests/invalid-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [enable NONEXISTING_KEY]
(no output to stdout)
1 change: 1 addition & 0 deletions cmd/enable/tests/missing-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [enable]
(no output to stdout)
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")
}
1 change: 1 addition & 0 deletions cmd/groups/tests/multiple-groups/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [groups]
(no output to stderr)
1 change: 1 addition & 0 deletions cmd/groups/tests/no-groups/stdout.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [groups]
(no output to stdout)
1 change: 1 addition & 0 deletions cmd/groups/tests/single-group/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [groups]
(no output to stderr)
4 changes: 2 additions & 2 deletions cmd/print/print_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 TestPrintCommand(t *testing.T) {
t.Parallel()

test_helpers.RunFileBasedCommandTests(t, test_helpers.SkipEnvCopy, "print")
test_helpers.RunFileBasedCommandTests(t, test_helpers.ReadOnly, "print")
}
1 change: 1 addition & 0 deletions cmd/print/tests/empty/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [print --no-color]
(no output to stderr)
1 change: 1 addition & 0 deletions cmd/print/tests/full/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [print --no-color --pretty]
(no output to stderr)
1 change: 1 addition & 0 deletions cmd/print/tests/simple-pretty/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [print --no-color --pretty]
(no output to stderr)
1 change: 1 addition & 0 deletions cmd/print/tests/simple/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [print --no-color]
(no output to stderr)
1 change: 1 addition & 0 deletions cmd/print/tests/specific-group/stderr.golden
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
---- exec command line 0: [print --no-color --group my-first-group]
(no output to stderr)
8 changes: 6 additions & 2 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package set

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -34,6 +33,9 @@ func NewCommand() *cobra.Command {

cmd.Flags().Bool("disabled", false, "Set/change the flag to be disabled (commented out)")
cmd.Flags().Bool("error-if-missing", false, "Exit with an error if the KEY does not exists in the .env file already")
cmd.Flags().Bool("skip-if-exists", false, "If the already KEY exists, do not set or change any settings")
cmd.Flags().Bool("skip-if-same", false, "If the already KEY exists, and it the value is identical, do not set or change any settings")

cmd.Flags().String("group", "", "The (optional) group name to add the KEY=VALUE pair under")
cmd.Flags().String("before", "", "If the key doesn't exist, add it to the file *before* this KEY")
cmd.Flags().String("after", "", "If the key doesn't exist, add it to the file *after* this KEY")
Expand Down Expand Up @@ -61,6 +63,8 @@ func runE(cmd *cobra.Command, args []string) error {
document,
upsert.WithGroup(shared.StringFlag(cmd.Flags(), "group")),
upsert.WithSettingIf(upsert.ErrorIfMissing, shared.BoolFlag(cmd.Flags(), "error-if-missing")),
upsert.WithSettingIf(upsert.SkipIfExists, shared.BoolFlag(cmd.Flags(), "skip-if-exists")),
upsert.WithSettingIf(upsert.SkipIfSame, shared.BoolFlag(cmd.Flags(), "skip-if-same")),
upsert.WithSettingIf(upsert.UpdateComments, cmd.Flag("comment").Changed),
)
if err != nil {
Expand Down Expand Up @@ -130,7 +134,7 @@ func runE(cmd *cobra.Command, args []string) error {
}

if allErrors != nil {
return errors.New("validation error")
return fmt.Errorf("validation error: %+w", allErrors)
}

//
Expand Down
2 changes: 1 addition & 1 deletion cmd/set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/jippi/dottie/pkg/test_helpers"
)

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

test_helpers.RunFileBasedCommandTests(t, 0, "set")
Expand Down
1 change: 1 addition & 0 deletions cmd/set/tests/error-if-missing.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOME_KEY="ERROR THE VALUE WAS NOT CHANGED"
2 changes: 2 additions & 0 deletions cmd/set/tests/error-if-missing.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--error-if-missing MISSING_KEY="THIS MUST FAIL"
--error-if-missing SOME_KEY="SUCCESS"
1 change: 1 addition & 0 deletions cmd/set/tests/error-if-missing/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOME_KEY="SUCCESS"
7 changes: 7 additions & 0 deletions cmd/set/tests/error-if-missing/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---- exec command line 0: [set --error-if-missing MISSING_KEY=THIS MUST FAIL]
(error *errors.errorString) key [MISSING_KEY] does not exists in the document

Error: validation error: key [MISSING_KEY] does not exists in the document
Run 'dottie set --help' for usage.
---- exec command line 1: [set --error-if-missing SOME_KEY=SUCCESS]
(no output to stderr)
5 changes: 5 additions & 0 deletions cmd/set/tests/error-if-missing/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---- exec command line 0: [set --error-if-missing MISSING_KEY=THIS MUST FAIL]
(no output to stdout)
---- exec command line 1: [set --error-if-missing SOME_KEY=SUCCESS]
Key [ SOME_KEY ] was successfully upserted
File was successfully saved
1 change: 0 additions & 1 deletion cmd/set/tests/manipulate-empty.env

This file was deleted.

10 changes: 9 additions & 1 deletion cmd/set/tests/manipulate-empty/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
---- exec command line 0: [set SOME_KEY=SOME_VALUE]
(no output to stderr)
---- exec command line 1: [set ANOTHER_KEY=ANOTHER_VALUE --quote-style single]
(no output to stderr)
---- exec command line 2: [set SECOND_KEY=should be before ANOTHER_KEY --before ANOTHER_KEY]
(no output to stderr)
---- exec command line 3: [set TRUE_SECOND_KEY=HA, I'm after SOME_KEY, so I'm before ANOTHER_KEY now --after SOME_KEY]
(no output to stderr)
---- exec command line 4: [set SECOND_KEY=damn, I'm the third key now]
(no output to stderr)
---- exec command line 5: [set SOME_KEY=ANOTHER_VALUE --comment I'm a comment --comment I'm another comment]
(no output to stderr)
---- exec command line 6: [set A_NUMBER=1 --comment @dottie/validate number]
(no output to stderr)
---- exec command line 7: [set NOT_A_NUMBER=abc --comment @dottie/validate number]
NOT_A_NUMBER (-:2)
* (number) The value [abc] is not a valid number.

Error: validation error
Error: validation error: Key: 'NOT_A_NUMBER' Error:Field validation for 'NOT_A_NUMBER' failed on the 'number' tag
Run 'dottie set --help' for usage.
---- exec command line 8: [set A_NUMBER=2]
(no output to stderr)
1 change: 1 addition & 0 deletions cmd/set/tests/manipulate-empty/stdout.golden
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ File was successfully saved
Key [ A_NUMBER ] was successfully upserted
File was successfully saved
---- exec command line 7: [set NOT_A_NUMBER=abc --comment @dottie/validate number]
(no output to stdout)
---- exec command line 8: [set A_NUMBER=2]
Key [ A_NUMBER ] was successfully upserted
File was successfully saved
2 changes: 2 additions & 0 deletions cmd/set/tests/skip-if-exists.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--skip-if-exists SOME_KEY="SUCCESS"
--skip-if-exists SOME_KEY="ERROR; THE VALUE MUST NOT CHANGE"
1 change: 1 addition & 0 deletions cmd/set/tests/skip-if-exists/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOME_KEY="SUCCESS"
4 changes: 4 additions & 0 deletions cmd/set/tests/skip-if-exists/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---- exec command line 0: [set --skip-if-exists SOME_KEY=SUCCESS]
(no output to stderr)
---- exec command line 1: [set --skip-if-exists SOME_KEY=ERROR; THE VALUE MUST NOT CHANGE]
(no output to stderr)
6 changes: 6 additions & 0 deletions cmd/set/tests/skip-if-exists/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---- exec command line 0: [set --skip-if-exists SOME_KEY=SUCCESS]
Key [ SOME_KEY ] was successfully upserted
File was successfully saved
---- exec command line 1: [set --skip-if-exists SOME_KEY=ERROR; THE VALUE MUST NOT CHANGE]
Key [ SOME_KEY ] was successfully upserted
File was successfully saved
2 changes: 2 additions & 0 deletions cmd/set/tests/skip-if-same.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--skip-if-same SOME_KEY="ERROR THIS SHOULD BE CHANGED BY NEXT LINE"
--skip-if-same SOME_KEY="SUCCESS"
1 change: 1 addition & 0 deletions cmd/set/tests/skip-if-same/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOME_KEY="SUCCESS"
4 changes: 4 additions & 0 deletions cmd/set/tests/skip-if-same/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---- exec command line 0: [set --skip-if-same SOME_KEY=ERROR THIS SHOULD BE CHANGED BY NEXT LINE]
(no output to stderr)
---- exec command line 1: [set --skip-if-same SOME_KEY=SUCCESS]
(no output to stderr)
6 changes: 6 additions & 0 deletions cmd/set/tests/skip-if-same/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---- exec command line 0: [set --skip-if-same SOME_KEY=ERROR THIS SHOULD BE CHANGED BY NEXT LINE]
Key [ SOME_KEY ] was successfully upserted
File was successfully saved
---- exec command line 1: [set --skip-if-same SOME_KEY=SUCCESS]
Key [ SOME_KEY ] was successfully upserted
File was successfully saved
18 changes: 9 additions & 9 deletions pkg/ast/upsert/upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,29 @@ func (u *Upserter) ApplyOptions(options ...Option) error {
// Upsert will, depending on its options, either Update or Insert (thus, "[Up]date + In[sert]").
func (u *Upserter) Upsert(ctx context.Context, input *ast.Assignment) (*ast.Assignment, error, error) {
assignment := u.document.Get(input.Name)
found := assignment != nil
exists := assignment != nil

// Short circuit with some quick settings checks

switch {
// The assignment exists, so return early
case found && u.settings.Has(SkipIfExists):
case exists && u.settings.Has(SkipIfExists):
return nil, nil, nil

// The assignment does *NOT* exists, and we require it to
case !exists && u.settings.Has(ErrorIfMissing):
return nil, nil, fmt.Errorf("key [%s] does not exists in the document", input.Name)

// The assignment exists, has a literal value, and the literal value isn't what we should consider empty
case found && u.settings.Has(SkipIfSet) && len(assignment.Literal) > 0 && !slices.Contains(u.valuesConsideredEmpty, assignment.Literal):
case exists && u.settings.Has(SkipIfSet) && len(assignment.Literal) > 0 && !slices.Contains(u.valuesConsideredEmpty, assignment.Literal):

Check warning on line 70 in pkg/ast/upsert/upsert.go

View check run for this annotation

Codecov / codecov/patch

pkg/ast/upsert/upsert.go#L70

Added line #L70 was not covered by tests
return nil, nil, nil

// The assignment exists, the literal values are the same, and they have same 'Enabled' level
case found && u.settings.Has(SkipIfSame) && assignment.Literal == input.Literal && assignment.Enabled == input.Enabled:
case exists && u.settings.Has(SkipIfSame) && assignment.Literal == input.Literal && assignment.Enabled == input.Enabled:

Check warning on line 74 in pkg/ast/upsert/upsert.go

View check run for this annotation

Codecov / codecov/patch

pkg/ast/upsert/upsert.go#L74

Added line #L74 was not covered by tests
return nil, nil, nil

// The assignment does *NOT* exists, and we require it to
case !found && u.settings.Has(ErrorIfMissing):
return nil, nil, fmt.Errorf("key [%s] does not exists in the document", input.Name)

// The KEY was *NOT* found, and all other preconditions are not triggering
case !found:
case !exists:
var err error

// Create and insert the (*ast.Assignment) into the Statement list
Expand Down
49 changes: 34 additions & 15 deletions pkg/test_helpers/filebased_command_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test_helpers
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -21,7 +22,7 @@ import (
type Setting int

const (
SkipEnvCopy Setting = 1 << iota
ReadOnly Setting = 1 << iota
)

// Has checks if [check] exists in the [settings] bitmask or not.
Expand Down Expand Up @@ -118,18 +119,23 @@ func RunFileBasedCommandTests(t *testing.T, settings Setting, globalArgs ...stri

dotEnvFile := "tests/" + tt.envFile

if !settings.Has(SkipEnvCopy) {
tmpDir := t.TempDir()
dotEnvFile = tmpDir + "/tmp.env"

// Copy the input.env to temporary place
err := copyFile(t, "tests/"+tt.envFile, tmpDir+"/tmp.env")
require.NoErrorf(t, err, "failed to copy [%s] to TempDir", tt.envFile)
if !settings.Has(ReadOnly) {
dotEnvFile = t.TempDir() + "/tmp.env"

if _, err := os.Stat("tests/" + tt.envFile); errors.Is(err, os.ErrNotExist) {
// Create a temporary empty .env file
_, err := os.Create(dotEnvFile)
require.NoErrorf(t, err, "failed to create empty .env file [ %s ] in TempDir", tt.envFile)
} else {
// Copy the input.env to temporary place
err := copyFile(t, "tests/"+tt.envFile, dotEnvFile)
require.NoErrorf(t, err, "failed to copy [ %s ] to TempDir", tt.envFile)
}
}

// Prepare output buffers
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
combinedStdout := bytes.Buffer{}
combinedStderr := bytes.Buffer{}

ctx := context.Background()

Expand All @@ -139,23 +145,36 @@ func RunFileBasedCommandTests(t *testing.T, settings Setting, globalArgs ...stri
args = append(args, globalArgs...)
args = append(args, command...)

stdout.WriteString(fmt.Sprintf("---- exec command line %d: %+v\n", idx, args))
stderr.WriteString(fmt.Sprintf("---- exec command line %d: %+v\n", idx, args))
combinedStdout.WriteString(fmt.Sprintf("---- exec command line %d: %+v\n", idx, args))
combinedStderr.WriteString(fmt.Sprintf("---- exec command line %d: %+v\n", idx, args))

commandArgs := append(args, "--file", dotEnvFile)

// Run command
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
out, _ := cmd.RunCommand(ctx, commandArgs, &stdout, &stderr)

if stdout.Len() == 0 {
stdout.WriteString("(no output to stdout)\n")
}

if stderr.Len() == 0 {
stderr.WriteString("(no output to stderr)\n")
}

stdout.WriteTo(&combinedStdout)
stderr.WriteTo(&combinedStderr)

// Assert we got a Cobra command back
require.NotNil(t, out, "expected a return value")
}

// Assert stdout + stderr + modified env file is as expected
golden.Assert(t, tt.goldenStdout, stdout.Bytes())
golden.Assert(t, tt.goldenStderr, stderr.Bytes())
golden.Assert(t, tt.goldenStdout, combinedStdout.Bytes())
golden.Assert(t, tt.goldenStderr, combinedStderr.Bytes())

if !settings.Has(SkipEnvCopy) {
if !settings.Has(ReadOnly) {
// Read the modified .env file back
modifiedEnv, err := os.ReadFile(dotEnvFile)

Expand Down

0 comments on commit 25a2079

Please sign in to comment.