Skip to content

Commit

Permalink
Merge branch 'master' into binary-read
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Apr 25, 2024
2 parents 9869797 + 31d38e0 commit 3c4c69e
Show file tree
Hide file tree
Showing 28 changed files with 2,761 additions and 962 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ cypress/screenshots
.idea
generated/

elm-review-report.gz.json
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"BackendTask.File",
"BackendTask.Custom",
"BackendTask.Env",
"BackendTask.Shell",
"Stream",
"BackendTask.Stream",
"BackendTask.Do",
"Server.Request",
"Server.Session",
Expand Down
17 changes: 9 additions & 8 deletions examples/end-to-end/review/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@
"dependencies": {
"direct": {
"elm/core": "1.0.5",
"jfmengels/elm-review": "2.8.1",
"jfmengels/elm-review-common": "1.2.1",
"jfmengels/elm-review-unused": "1.1.21",
"stil4m/elm-syntax": "7.2.9"
"jfmengels/elm-review": "2.13.1",
"jfmengels/elm-review-common": "1.3.3",
"jfmengels/elm-review-unused": "1.2.0",
"mthadley/elm-review-unit": "2.0.2",
"stil4m/elm-syntax": "7.3.2"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/project-metadata-utils": "1.0.2",
"elm/random": "1.0.0",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.3",
"elm-community/list-extra": "8.6.0",
"elm-explorations/test": "1.2.2",
"miniBill/elm-unicode": "1.0.2",
"elm-explorations/test": "2.2.0",
"miniBill/elm-unicode": "1.1.1",
"rtfeldman/elm-hex": "1.0.0",
"stil4m/structured-writer": "1.0.3"
}
},
"test-dependencies": {
"direct": {
"elm-explorations/test": "1.2.2"
"elm-explorations/test": "2.2.0"
},
"indirect": {}
}
Expand Down
42 changes: 42 additions & 0 deletions examples/end-to-end/script/custom-backend-task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Writable, Transform, Readable } from "node:stream";

export async function hello(input, { cwd, env }) {
return `Hello!`;
}

export async function upperCaseStream() {
return {
metadata: () => "Hi! I'm metadata from upperCaseStream!",
stream: new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk.toString().toUpperCase());
},
}),
};
}

export async function customReadStream() {
return new Readable({
read(size) {
this.push("Hello from customReadStream!");
this.push(null);
},
});
}

export async function customWrite(input) {
return {
stream: stdout(),
metadata: () => {
return "Hi! I'm metadata from customWriteStream!";
},
};
}

function stdout() {
return new Writable({
write(chunk, encoding, callback) {
process.stdout.write(chunk, callback);
},
});
}
8 changes: 5 additions & 3 deletions examples/end-to-end/script/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.3",
"elm-community/list-extra": "8.7.0",
"elm-explorations/test": "2.2.0",
"jluckyiv/elm-utc-date-strings": "1.0.0",
"justinmimbs/date": "4.0.1",
"mdgriffith/elm-codegen": "4.1.1",
"justinmimbs/date": "4.1.0",
"mdgriffith/elm-codegen": "4.2.1",
"miniBill/elm-codec": "2.1.0",
"noahzgordon/elm-color-extra": "1.0.2",
"robinheghan/fnv1a": "1.0.0",
Expand All @@ -42,7 +44,7 @@
"elm-community/basics-extra": "4.1.0",
"elm-community/maybe-extra": "5.3.0",
"fredcy/elm-parseint": "2.0.1",
"miniBill/elm-unicode": "1.1.0",
"miniBill/elm-unicode": "1.1.1",
"robinheghan/murmur3": "1.0.0",
"rtfeldman/elm-hex": "1.0.0",
"rtfeldman/elm-iso8601-date-strings": "1.1.4",
Expand Down
258 changes: 258 additions & 0 deletions examples/end-to-end/script/src/BackendTaskTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
module BackendTaskTest exposing (run, testScript)

import Array exposing (Array)
import BackendTask exposing (BackendTask)
import BackendTask.Random
import Expect exposing (Expectation)
import FatalError exposing (FatalError)
import List.Extra
import Pages.Script as Script exposing (Script)
import Random
import Test exposing (Test)
import Test.Runner exposing (getFailureReason)
import Test.Runner.Failure exposing (InvalidReason, Reason(..))


testScript : String -> List (BackendTask FatalError Test.Test) -> Script
testScript suiteName testCases =
testCases
|> BackendTask.sequence
|> BackendTask.map (Test.describe suiteName)
|> run
|> Script.withoutCliOptions


run : BackendTask FatalError Test -> BackendTask FatalError ()
run toTest =
BackendTask.Random.int32
|> BackendTask.map Random.initialSeed
|> BackendTask.andThen
(\seed ->
toTest
|> BackendTask.andThen
(\testCase ->
case Test.Runner.fromTest 1 seed testCase of
Test.Runner.Plain tests ->
case toFailures tests of
[] ->
Script.log (green "✔️ All tests passed!")

failures ->
BackendTask.fail
(FatalError.build
{ title = "Test suite failed"
, body =
failures
|> List.map
(\( label, failure ) ->
"X " ++ label ++ "\n>>>>>> \n " ++ failure ++ "\n<<<<<<\n"
)
|> String.join "\n\n"
}
)

Test.Runner.Only tests ->
case toFailures tests of
[] ->
BackendTask.fail
(FatalError.build
{ title = "Passed With Only"
, body = "The test suite passed, but only was used."
}
)

failures ->
BackendTask.fail
(FatalError.build
{ title = "Test suite failed"
, body =
failures
|> List.map
(\( label, failure ) ->
label ++ " | " ++ failure
)
|> String.join "\n"
}
)

Test.Runner.Skipping tests ->
case toFailures tests of
[] ->
BackendTask.fail
(FatalError.build
{ title = "Passed With Skip"
, body = "The test suite passed, but some tests were skipped."
}
)

failures ->
BackendTask.fail
(FatalError.build
{ title = "Test suite failed"
, body =
failures
|> List.map
(\( label, failure ) ->
label ++ " | " ++ failure
)
|> String.join "\n"
}
)

Test.Runner.Invalid string ->
BackendTask.fail
(FatalError.build
{ title = "Invalid test suite"
, body = string
}
)
)
)


toFailures tests =
let
resultsWithLabels : List ( String, Expectation )
resultsWithLabels =
List.Extra.zip
(tests |> List.concatMap (\test -> test.labels))
(tests |> List.concatMap (\test -> test.run ()))

failures : List ( String, Maybe String )
failures =
resultsWithLabels
|> List.map
(Tuple.mapSecond
(\thing ->
thing
|> getFailureReason
|> Maybe.map
(\failure ->
viewReason failure.reason
)
)
)

onlyFailures : List ( String, String )
onlyFailures =
List.filterMap
(\( label, maybeFailure ) ->
case maybeFailure of
Just failure ->
Just ( label, failure )

Nothing ->
Nothing
)
failures
in
onlyFailures


viewReason : Reason -> String
viewReason reason =
case reason of
Custom ->
"Custom"

Equality expected actual ->
"Expected: " ++ expected ++ " | Actual: " ++ actual

Comparison expected actual ->
"Expected: " ++ expected ++ " | Actual: " ++ actual

ListDiff expected received ->
viewListDiff expected received

CollectionDiff details ->
"Expected: " ++ details.expected ++ " | Actual: " ++ details.actual

TODO ->
"TODO"

Invalid invalidReason ->
viewInvalidReason invalidReason


viewInvalidReason : InvalidReason -> String
viewInvalidReason reason =
case reason of
Test.Runner.Failure.EmptyList ->
"You should have at least one test in the list"

Test.Runner.Failure.NonpositiveFuzzCount ->
"The fuzz count must be positive"

Test.Runner.Failure.InvalidFuzzer ->
"The fuzzer used is invalid"

Test.Runner.Failure.BadDescription ->
"The description of your test is not valid"

Test.Runner.Failure.DuplicatedName ->
"At least two tests have the same name, please change at least one"

Test.Runner.Failure.DistributionInsufficient ->
"The distribution is not sufficient"

Test.Runner.Failure.DistributionBug ->
"The distribution is not correct"


viewListDiff : List String -> List String -> String
viewListDiff expected actual =
let
expectedArray : Array String
expectedArray =
Array.fromList expected

actualArray : Array String
actualArray =
Array.fromList actual
in
"The lists don't match!"
++ "Expected"
++ (List.indexedMap (viewListDiffPart actualArray) expected |> String.join " ")
++ "Actual"
++ (List.indexedMap (viewListDiffPart expectedArray) actual |> String.join " ")


viewListDiffPart : Array String -> Int -> String -> String
viewListDiffPart otherList index listPart =
let
isGreen : Bool
isGreen =
Array.get index otherList
|> maybeFilter (\value -> value == listPart)
|> Maybe.map (always True)
|> Maybe.withDefault False
in
if isGreen then
green listPart

else
red listPart


maybeFilter : (a -> Bool) -> Maybe a -> Maybe a
maybeFilter f m =
case m of
Just a ->
if f a then
m

else
Nothing

Nothing ->
Nothing


green : String -> String
green text =
"\u{001B}[32m" ++ text ++ "\u{001B}[0m"


red : String -> String
red text =
"\u{001B}[31m" ++ text ++ "\u{001B}[0m"
Empty file.
14 changes: 14 additions & 0 deletions examples/end-to-end/script/src/HttpStreamDemo.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Todo exposing (run)

import BackendTask
import Pages.Script as Script exposing (Script)


run : Script
run =
Script.log "Just"
|> BackendTask.andThen
(\_ ->
Debug.todo "Error string from todo."
)
|> Script.withoutCliOptions
Loading

0 comments on commit 3c4c69e

Please sign in to comment.