Skip to content

Commit

Permalink
Avoid name clashes when Main module is used as a test dependency. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
kczulko authored May 18, 2023
1 parent cea8ebc commit 944cd84
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 15 deletions.
3 changes: 2 additions & 1 deletion elm/generate_test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ def visit_tvar(self, name):
# TODO(edsch): What about the seed?
with open(sys.argv[2], "w") as f:
print(
"""import Console.Text exposing (UseColor(..))
"""module RulesElmMainTestsExecutor exposing (main)
import Console.Text exposing (UseColor(..))
import Test
import Test.Reporter.Reporter exposing (Report(..))
import Test.Runner.Node
Expand Down
2 changes: 1 addition & 1 deletion elm/run_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function handleResults(response) {
}
}

var app = require(process.argv[2]).Elm.Main.init({
var app = require(process.argv[2]).Elm.RulesElmMainTestsExecutor.init({
flags: 0,
})

Expand Down
2 changes: 1 addition & 1 deletion examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ rules_elm_integration_test_each_bazel(
expected_output = """TEST RUN PASSED
Duration: 0 ms
Passed: 3
Passed: 4
Failed: 0""",
)

Expand Down
10 changes: 10 additions & 0 deletions examples/elm-test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ elm_library(
deps = [],
)

elm_library(
name = "main-lib",
srcs = ["Main.elm"],
deps = [
"@elm_package_elm_browser",
"@elm_package_elm_http",
],
)

elm_test(
name = "spec",
main = "Spec.elm",
node = "@nodejs//:bin/node",
deps = [
":lib",
":main-lib",
"@elm_package_elm_core",
"@elm_package_elm_json",
"@elm_package_elm_test",
Expand Down
86 changes: 86 additions & 0 deletions examples/elm-test/Main.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module Main exposing (main, init, someFunctionAlwaysReturning5)

import Browser
import Html exposing (Html, text, pre)
import Http

-- MAIN


main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}



-- MODEL


type Model
= Failure
| Loading
| Success String


init : () -> (Model, Cmd Msg)
init _ =
( Loading
, Http.get
{ url = "https://elm-lang.org/assets/public-opinion.txt"
, expect = Http.expectString GotText
}
)



-- UPDATE


type Msg
= GotText (Result Http.Error String)


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
GotText result ->
case result of
Ok fullText ->
(Success fullText, Cmd.none)

Err _ ->
(Failure, Cmd.none)



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none



-- VIEW


view : Model -> Html Msg
view model =
case model of
Failure ->
text "I was unable to load your book."

Loading ->
text "Loading..."

Success fullText ->
pre [] [ text fullText ]



someFunctionAlwaysReturning5 = 5
3 changes: 3 additions & 0 deletions examples/elm-test/Spec.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Spec exposing(..)
import Expect
import Test exposing (..)
import Lib exposing(plus2)
import Main

additionTests =
describe "Addition"
Expand All @@ -12,5 +13,7 @@ additionTests =
\_ -> (3 + 4) |> Expect.equal 7
, test "plus2 adds two to the argument" <|
\_ -> plus2 2 |> Expect.equal 4
, test "This specific Main module function always returns 5" <|
\_ -> Main.someFunctionAlwaysReturning5 |> Expect.equal 5
]

48 changes: 36 additions & 12 deletions examples/elm-test/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@ nixpkgs_package(
repository = "@nixpkgs",
)

load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_java_configure")
nixpkgs_java_configure(
attribute_path = "jdk11.home",
repository = "@nixpkgs",
toolchain = True,
toolchain_name = "nixpkgs_java",
toolchain_version = "11",
)

elm_repository(
name = "elm_package_elm_core",
sha256 = "6e37b11c88c89a68d19d0c7625f1ef39ed70c59e443def95e4de98d6748c80a7",
strip_prefix = "core-1.0.5",
urls = ["https://github.com/elm/core/archive/1.0.5.tar.gz"],
name = "elm_package_elm_core",
sha256 = "6e37b11c88c89a68d19d0c7625f1ef39ed70c59e443def95e4de98d6748c80a7",
strip_prefix = "core-1.0.5",
urls = ["https://github.com/elm/core/archive/1.0.5.tar.gz"],
)
elm_repository(
name = "elm_package_elm_json",
Expand Down Expand Up @@ -95,12 +104,27 @@ elm_repository(
strip_prefix = "time-1.0.0",
urls = ["https://github.com/elm/time/archive/1.0.0.tar.gz"],
)

load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_java_configure")
nixpkgs_java_configure(
attribute_path = "jdk11.home",
repository = "@nixpkgs",
toolchain = True,
toolchain_name = "nixpkgs_java",
toolchain_version = "11",
elm_repository(
name = "elm_package_elm_http",
sha256 = "619bc23d7753bc172016ea764233dd7dfded1d919263c41b59885c5bcdd10b01",
strip_prefix = "http-2.0.0",
urls = ["https://github.com/elm/http/archive/2.0.0.tar.gz"],
)
elm_repository(
name = "elm_package_elm_file",
sha256 = "c85b4025e12c1bf2dee9e4d853459ead7d1fa917304adfa2af27d116c86292e6",
strip_prefix = "file-1.0.5",
urls = ["https://github.com/elm/file/archive/1.0.5.tar.gz"],
)
elm_repository(
name = "elm_package_elm_browser",
sha256 = "23f41491d325afc72649d512741fb8173725014c93e482d25bab3325555a4f59",
strip_prefix = "browser-1.0.2",
urls = ["https://github.com/elm/browser/archive/1.0.2.tar.gz"],
)
elm_repository(
name = "elm_package_elm_url",
sha256 = "840e9d45d8a9bd64a7f76421a1de2518e02c7cbea7ed42efd380b4e875e9682b",
strip_prefix = "url-1.0.0",
urls = ["https://github.com/elm/url/archive/1.0.0.tar.gz"],
)

0 comments on commit 944cd84

Please sign in to comment.