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 453fec8 commit 67cd5ab
Show file tree
Hide file tree
Showing 32 changed files with 151 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
---- exec command line 0: [disable KEY_B]
WARNING: The key [KEY_B] is already disabled
---- done command line 0: [disable KEY_B]
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [disable KEY_B]
---- done command line 0: [disable KEY_B]
1 change: 0 additions & 1 deletion cmd/disable/tests/disable-a-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [disable KEY_B]
---- done command line 0: [disable KEY_B]
1 change: 0 additions & 1 deletion cmd/disable/tests/disable-a-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
---- exec command line 0: [disable KEY_B]
Key [KEY_B] was successfully disabled
---- done command line 0: [disable KEY_B]
1 change: 0 additions & 1 deletion cmd/disable/tests/invalid-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---- exec command line 0: [disable NONEXISTING_KEY]
Error: Could not find KEY [NONEXISTING_KEY]
Run 'dottie disable --help' for usage.
---- done command line 0: [disable NONEXISTING_KEY]
1 change: 0 additions & 1 deletion cmd/disable/tests/invalid-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [disable NONEXISTING_KEY]
---- done command line 0: [disable NONEXISTING_KEY]
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
---- exec command line 0: [enable KEY_B]
WARNING: The key [KEY_B] is already enabled
---- done command line 0: [enable KEY_B]
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
---- exec command line 0: [enable KEY_B]
Key [KEY_B] was successfully enabled
---- done command line 0: [enable KEY_B]
1 change: 0 additions & 1 deletion cmd/enable/tests/enable-a-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [enable KEY_B]
---- done command line 0: [enable KEY_B]
1 change: 0 additions & 1 deletion cmd/enable/tests/enable-a-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
---- exec command line 0: [enable KEY_B]
Key [KEY_B] was successfully enabled
---- done command line 0: [enable KEY_B]
1 change: 0 additions & 1 deletion cmd/enable/tests/invalid-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---- exec command line 0: [enable NONEXISTING_KEY]
Error: Could not find KEY [NONEXISTING_KEY]
Run 'dottie enable --help' for usage.
---- done command line 0: [enable NONEXISTING_KEY]
1 change: 0 additions & 1 deletion cmd/enable/tests/invalid-key/stdout.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [enable NONEXISTING_KEY]
---- done command line 0: [enable NONEXISTING_KEY]
1 change: 0 additions & 1 deletion cmd/print/tests/empty/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [print --no-color]
---- done command line 0: [print --no-color]
1 change: 0 additions & 1 deletion cmd/print/tests/empty/stdout.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---- exec command line 0: [print --no-color]


---- done command line 0: [print --no-color]
1 change: 0 additions & 1 deletion cmd/print/tests/full/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [print --pretty --no-color]
---- done command line 0: [print --pretty --no-color]
1 change: 0 additions & 1 deletion cmd/print/tests/full/stdout.golden
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ GROUP_TWO_A="WORLD"

SECRET=KEY

---- done command line 0: [print --pretty --no-color]
1 change: 0 additions & 1 deletion cmd/print/tests/simple-pretty/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [print --no-color]
---- done command line 0: [print --no-color]
1 change: 0 additions & 1 deletion cmd/print/tests/simple-pretty/stdout.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ KEY_A="I'm key A"
KEY_B="I'm key B"
KEY_C="I'm key C"

---- done command line 0: [print --no-color]
1 change: 0 additions & 1 deletion cmd/print/tests/simple/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [print --no-color]
---- done command line 0: [print --no-color]
1 change: 0 additions & 1 deletion cmd/print/tests/simple/stdout.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ KEY_A="I'm key A"
KEY_B="I'm key B"
KEY_C="I'm key C"

---- done command line 0: [print --no-color]
1 change: 0 additions & 1 deletion cmd/print/tests/specific-group/stderr.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
---- exec command line 0: [print --no-color --group my-first-group]
---- done command line 0: [print --no-color --group my-first-group]
1 change: 0 additions & 1 deletion cmd/print/tests/specific-group/stdout.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ KEY_A="I'm key A"
KEY_B="I'm key B"
KEY_C="I'm key C"

---- done command line 0: [print --no-color --group my-first-group]
16 changes: 9 additions & 7 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package set
import (
"errors"
"fmt"
"os"
"strings"

"github.com/jippi/dottie/pkg"
Expand Down Expand Up @@ -87,7 +86,10 @@ func runE(cmd *cobra.Command, args []string) error {
// Loop arguments and place them
//

var allErrors error
var (
allErrors error
stdout, stderr = tui.PrintersFromContext(cmd.Context())
)

for _, stringPair := range args {
pairSlice := strings.SplitN(stringPair, "=", 2)
Expand All @@ -106,7 +108,7 @@ func runE(cmd *cobra.Command, args []string) error {
Interpolated: value,
Enabled: !shared.BoolFlag(cmd.Flags(), "disabled"),
Quote: token.QuoteFromString(shared.StringFlag(cmd.Flags(), "quote-style")),
Comments: ast.NewCommentsFromSlice(shared.StringSliceFlag(cmd.Flags(), "comments")),
Comments: ast.NewCommentsFromSlice(shared.StringSliceFlag(cmd.Flags(), "comment")),
}

//
Expand All @@ -115,12 +117,12 @@ func runE(cmd *cobra.Command, args []string) error {

assignment, warnings, err := upserter.Upsert(assignment)
if warnings != nil {
tui.Theme.Warning.StderrPrinter().Println("WARNING:", warnings)
stderr.Color(tui.Warning).Println("WARNING:", warnings)

Check warning on line 120 in cmd/set/set.go

View check run for this annotation

Codecov / codecov/patch

cmd/set/set.go#L120

Added line #L120 was not covered by tests
}

if err != nil {
z := validation.NewError(assignment, err)
fmt.Fprintln(os.Stderr, validation.Explain(document, z, z, false, true))
stderr.Color(tui.Neutral).Println(validation.Explain(document, z, z, false, true))

if shared.BoolWithInverseValue(cmd.Flags(), "validate") {
allErrors = multierr.Append(allErrors, err)
Expand All @@ -129,7 +131,7 @@ func runE(cmd *cobra.Command, args []string) error {
}
}

tui.Theme.Success.StderrPrinter().Printfln("Key [%s] was successfully upserted", key)
stdout.Color(tui.Success).Printfln("Key [%s] was successfully upserted", key)
}

if allErrors != nil {
Expand All @@ -144,7 +146,7 @@ func runE(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to save file: %w", err)
}

tui.Theme.Success.StderrPrinter().Println("File was successfully saved")
stdout.Color(tui.Success).Println("File was successfully saved")

return nil
}
13 changes: 13 additions & 0 deletions cmd/set/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package set_test

import (
"testing"

"github.com/jippi/dottie/pkg/test_helpers"
)

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

test_helpers.RunFileBasedCommandTests(t, 0, "set")
}
1 change: 1 addition & 0 deletions cmd/set/tests/manipulate-empty.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOME_KEY="SOME_VALUE"
9 changes: 9 additions & 0 deletions cmd/set/tests/manipulate-empty.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SOME_KEY=SOME_VALUE
ANOTHER_KEY=ANOTHER_VALUE --quote-style single
SECOND_KEY="should be before ANOTHER_KEY" --before ANOTHER_KEY
TRUE_SECOND_KEY="HA, I'm after SOME_KEY, so I'm before ANOTHER_KEY now" --after SOME_KEY
SECOND_KEY="damn, I'm the third key now"
SOME_KEY=ANOTHER_VALUE --comment "I'm a comment" --comment "I'm another comment"
A_NUMBER=1 --comment "@dottie/validate number"
NOT_A_NUMBER=abc --comment "@dottie/validate number"
A_NUMBER=2
10 changes: 10 additions & 0 deletions cmd/set/tests/manipulate-empty/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# I'm a comment
# I'm another comment
SOME_KEY="ANOTHER_VALUE"

TRUE_SECOND_KEY="HA, I'm after SOME_KEY, so I'm before ANOTHER_KEY now"
SECOND_KEY="damn, I'm the third key now"
ANOTHER_KEY='ANOTHER_VALUE'

# @dottie/validate number
A_NUMBER="2"
14 changes: 14 additions & 0 deletions cmd/set/tests/manipulate-empty/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---- exec command line 0: [set SOME_KEY=SOME_VALUE]
---- exec command line 1: [set ANOTHER_KEY=ANOTHER_VALUE --quote-style single]
---- exec command line 2: [set SECOND_KEY=should be before ANOTHER_KEY --before ANOTHER_KEY]
---- exec command line 3: [set TRUE_SECOND_KEY=HA, I'm after SOME_KEY, so I'm before ANOTHER_KEY now --after SOME_KEY]
---- exec command line 4: [set SECOND_KEY=damn, I'm the third key now]
---- exec command line 5: [set SOME_KEY=ANOTHER_VALUE --comment I'm a comment --comment I'm another comment]
---- exec command line 6: [set A_NUMBER=1 --comment @dottie/validate number]
---- 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
Run 'dottie set --help' for usage.
---- exec command line 8: [set A_NUMBER=2]
25 changes: 25 additions & 0 deletions cmd/set/tests/manipulate-empty/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---- exec command line 0: [set SOME_KEY=SOME_VALUE]
Key [SOME_KEY] was successfully upserted
File was successfully saved
---- exec command line 1: [set ANOTHER_KEY=ANOTHER_VALUE --quote-style single]
Key [ANOTHER_KEY] was successfully upserted
File was successfully saved
---- exec command line 2: [set SECOND_KEY=should be before ANOTHER_KEY --before ANOTHER_KEY]
Key [SECOND_KEY] was successfully upserted
File was successfully saved
---- exec command line 3: [set TRUE_SECOND_KEY=HA, I'm after SOME_KEY, so I'm before ANOTHER_KEY now --after SOME_KEY]
Key [TRUE_SECOND_KEY] was successfully upserted
File was successfully saved
---- exec command line 4: [set SECOND_KEY=damn, I'm the third key now]
Key [SECOND_KEY] was successfully upserted
File was successfully saved
---- exec command line 5: [set SOME_KEY=ANOTHER_VALUE --comment I'm a comment --comment I'm another comment]
Key [SOME_KEY] was successfully upserted
File was successfully saved
---- exec command line 6: [set A_NUMBER=1 --comment @dottie/validate number]
Key [A_NUMBER] was successfully upserted
File was successfully saved
---- exec command line 7: [set NOT_A_NUMBER=abc --comment @dottie/validate number]
---- exec command line 8: [set A_NUMBER=2]
Key [A_NUMBER] was successfully upserted
File was successfully saved
37 changes: 37 additions & 0 deletions pkg/ast/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,40 @@ func (document *Document) Initialize() {
}
}
}

func (document *Document) Replace(assignment *Assignment) error {
existing := document.Get(assignment.Name)
if existing == nil {
return fmt.Errorf("No KEY named [%s] exists in the document", assignment.Name)

Check warning on line 302 in pkg/ast/document.go

View check run for this annotation

Codecov / codecov/patch

pkg/ast/document.go#L302

Added line #L302 was not covered by tests
}

if existing.Group != nil {
for idx, stmt := range existing.Group.Statements {
val, ok := stmt.(*Assignment)
if !ok {
continue

Check warning on line 309 in pkg/ast/document.go

View check run for this annotation

Codecov / codecov/patch

pkg/ast/document.go#L306-L309

Added lines #L306 - L309 were not covered by tests
}

if val.Name == assignment.Name {
existing.Group.Statements[idx] = assignment

Check warning on line 313 in pkg/ast/document.go

View check run for this annotation

Codecov / codecov/patch

pkg/ast/document.go#L312-L313

Added lines #L312 - L313 were not covered by tests

return nil

Check warning on line 315 in pkg/ast/document.go

View check run for this annotation

Codecov / codecov/patch

pkg/ast/document.go#L315

Added line #L315 was not covered by tests
}
}
}

for idx, stmt := range document.Statements {
val, ok := stmt.(*Assignment)
if !ok {
continue
}

if val.Name == assignment.Name {
document.Statements[idx] = assignment

return nil
}
}

return fmt.Errorf("Could not find+replace KEY named [%s] in document", assignment.Name)

Check warning on line 333 in pkg/ast/document.go

View check run for this annotation

Codecov / codecov/patch

pkg/ast/document.go#L333

Added line #L333 was not covered by tests
}
42 changes: 33 additions & 9 deletions pkg/ast/upsert/upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"slices"

"github.com/jippi/dottie/pkg/ast"
"github.com/jippi/dottie/pkg/parser"
"github.com/jippi/dottie/pkg/render"
"github.com/jippi/dottie/pkg/scanner"
"github.com/jippi/dottie/pkg/validation"
"go.uber.org/multierr"
)
Expand Down Expand Up @@ -79,9 +82,6 @@ func (u *Upserter) Upsert(input *ast.Assignment) (*ast.Assignment, error, error)
if err != nil {
return nil, nil, err
}

// Recalculate the index order of all Statements (for interpolation)
u.document.ReindexStatements()
}

// Replace comments on the assignment if the Setting is on
Expand All @@ -93,15 +93,32 @@ func (u *Upserter) Upsert(input *ast.Assignment) (*ast.Assignment, error, error)
assignment.Literal = input.Literal
assignment.Quote = input.Quote
assignment.Interpolated = input.Literal

var (
tempDoc *ast.Document
err, warnings error
)

// Render and parse back the Statement to ensure annotations and such are properly handled
tempDoc, err = parser.New(scanner.New(render.NewFormatter().Statement(assignment).String()), "-").Parse()
if err != nil {
return nil, nil, fmt.Errorf("failed to parse assignment: %w", err)

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

View check run for this annotation

Codecov / codecov/patch

pkg/ast/upsert/upsert.go#L105

Added line #L105 was not covered by tests
}

assignment = tempDoc.Get(assignment.Name)
assignment.Initialize()

if _, ok := assignment.Dependencies[assignment.Name]; ok {
return nil, nil, fmt.Errorf("Key [%s] may not reference itself!", assignment.Name)
}

u.document.Initialize()
// Replace the Assignment in the document
//
// This is necessary since its a different pointer address after we rendered+parsed earlier
u.document.Replace(assignment)

var err, warnings error
// Reinitialize the document so all indices and such are correct
u.document.Initialize()

// Interpolate the Assignment if it is enabled
if assignment.Enabled {
Expand All @@ -128,18 +145,25 @@ func (u *Upserter) Upsert(input *ast.Assignment) (*ast.Assignment, error, error)
}

func (u *Upserter) createAndInsert(input *ast.Assignment) (*ast.Assignment, error) {
// Ensure the group exists (may return 'nil' if no group is required)
group := u.document.EnsureGroup(u.group)

// Create the new newAssignment
newAssignment := &ast.Assignment{
Comments: input.Comments,
Enabled: input.Enabled,
Group: group,
Literal: input.Literal,
Name: input.Name,
}

doc, err := parser.New(scanner.New(render.NewFormatter().Statement(newAssignment).String()), "-").Parse()
if err != nil {
return nil, fmt.Errorf("failed to parse assignment: %w", err)

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

View check run for this annotation

Codecov / codecov/patch

pkg/ast/upsert/upsert.go#L158

Added line #L158 was not covered by tests
}

// Ensure the group exists (may return 'nil' if no group is required)
group := u.document.EnsureGroup(u.group)

newAssignment = doc.Get(newAssignment.Name)
newAssignment.Group = group

// Find the statement slice to operate on
statements := u.document.Statements
if newAssignment.Group != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/test_helpers/filebased_command_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ func RunFileBasedCommandTests(t *testing.T, settings Setting, globalArgs ...stri

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

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

// Assert stdout + stderr + modified env file is as expected
Expand Down

0 comments on commit 67cd5ab

Please sign in to comment.