Skip to content

Commit

Permalink
Auto-skip: add support for loops (#3372)
Browse files Browse the repository at this point in the history
Basic AutoSkip support for `FOR...IN`.
  • Loading branch information
mikejholly authored Oct 13, 2023
1 parent 61d3751 commit 4dbbb1d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
26 changes: 17 additions & 9 deletions inputgraph/inputgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ func (l *loader) handleIf(ctx context.Context, ifStmt spec.IfStatement) error {
}

func (l *loader) handleFor(ctx context.Context, forStmt spec.ForStatement) error {
return errors.Wrap(ErrUnableToDetermineHash, "for not supported")
l.hashFor(forStmt)
err := l.loadBlock(ctx, forStmt.Body)
if err != nil {
return err
}
return nil
}

func (l *loader) handleWait(ctx context.Context, waitStmt spec.WaitStatement) error {
Expand Down Expand Up @@ -256,7 +261,6 @@ func (l *loader) loadBlock(ctx context.Context, b spec.Block) error {
}

func (l *loader) hashIfStatement(s spec.IfStatement) {
s.SourceLocation = nil
l.hasher.HashString("IF")
l.hasher.HashJSONMarshalled(s.Expression)
l.hasher.HashBool(s.ExecMode)
Expand All @@ -268,28 +272,32 @@ func (l *loader) hashIfStatement(s spec.IfStatement) {
}

func (l *loader) hashElseIf(e spec.ElseIf) {
e.SourceLocation = nil
l.hasher.HashString("ELSE IF")
l.hasher.HashJSONMarshalled(e.Expression)
l.hasher.HashBool(e.ExecMode)
l.hasher.HashInt(len(e.Body))
}

func (l *loader) hashWaitStatement(w spec.WaitStatement) {
w.SourceLocation = nil
l.hasher.HashString("WAIT")
l.hasher.HashInt(len(w.Body))
l.hasher.HashJSONMarshalled(w.Args)
}

func (l *loader) hashVersion(v spec.Version) {
v.SourceLocation = nil
l.hasher.HashJSONMarshalled(v)
l.hasher.HashString("VERSION")
l.hasher.HashJSONMarshalled(v.Args)
}

func (l *loader) hashCommand(c spec.Command) {
l.hasher.HashString(c.Name)
l.hasher.HashJSONMarshalled(c.Args)
l.hasher.HashBool(c.ExecMode)
}

func (l *loader) hashCommand(cmd spec.Command) {
cmd.SourceLocation = nil
l.hasher.HashJSONMarshalled(cmd)
func (l *loader) hashFor(f spec.ForStatement) {
l.hasher.HashString("FOR")
l.hasher.HashJSONMarshalled(f.Args)
}

func copyVisited(m map[string]struct{}) map[string]struct{} {
Expand Down
9 changes: 7 additions & 2 deletions tests/autoskip/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test-all:
BUILD +test-auto-skip-requires-project
BUILD +test-auto-skip-wait
BUILD +test-auto-skip-if-else
BUILD +test-auto-skip-for-in

test-auto-skip:
RUN echo hello > my-file
Expand Down Expand Up @@ -45,11 +46,15 @@ test-auto-skip-requires-project:

test-auto-skip-wait:
DO --pass-args +RUN_EARTHLY_ARGS --earthfile=wait.earth --target=+test --output_contains="not skipped"
DO --pass-args +RUN_EARTHLY_ARGS --earthfile=wait.earth --target=+test --output_contains="ec3d61867365de2deda79ce06f7afa849b765bb1"
DO --pass-args +RUN_EARTHLY_ARGS --earthfile=wait.earth --target=+test --output_contains="8be037baa7b4d09a8e2a37f74154d319d64c996a"

test-auto-skip-if-else:
DO --pass-args +RUN_EARTHLY_ARGS --earthfile=if-else.earth --target=+test --output_contains="condition ok"
DO --pass-args +RUN_EARTHLY_ARGS --earthfile=if-else.earth --target=+test --output_contains="d7ba3699b3342c386fd40b3368c167898ae58f2a"
DO --pass-args +RUN_EARTHLY_ARGS --earthfile=if-else.earth --target=+test --output_contains="40f57fc7955914f3a954c6bd9a2ebe48d0b14f40"

test-auto-skip-for-in:
DO --pass-args +RUN_EARTHLY_ARGS --earthfile=for.earth --target=+test --output_contains="hello 3"
DO --pass-args +RUN_EARTHLY_ARGS --earthfile=for.earth --target=+test --output_contains="3d7d7fec7efc5a746e9ac658160427decbf58a0b"

RUN_EARTHLY_ARGS:
COMMAND
Expand Down
10 changes: 10 additions & 0 deletions tests/autoskip/for.earth
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
VERSION 0.7

PROJECT earthly-technologies/core

FROM alpine

test:
FOR idx IN "1 2 3"
RUN echo "hello $idx"
END

0 comments on commit 4dbbb1d

Please sign in to comment.