From 944cd84f4e0dd101e9d632ca6fb2ceee15e8509a Mon Sep 17 00:00:00 2001 From: kczulko Date: Thu, 18 May 2023 15:14:09 +0200 Subject: [PATCH] Avoid name clashes when Main module is used as a test dependency. (#17) --- elm/generate_test_main.py | 3 +- elm/run_test.js | 2 +- examples/BUILD.bazel | 2 +- examples/elm-test/BUILD.bazel | 10 ++++ examples/elm-test/Main.elm | 86 +++++++++++++++++++++++++++++++++++ examples/elm-test/Spec.elm | 3 ++ examples/elm-test/WORKSPACE | 48 ++++++++++++++----- 7 files changed, 139 insertions(+), 15 deletions(-) create mode 100644 examples/elm-test/Main.elm diff --git a/elm/generate_test_main.py b/elm/generate_test_main.py index bd095b3..e92dcd8 100644 --- a/elm/generate_test_main.py +++ b/elm/generate_test_main.py @@ -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 diff --git a/elm/run_test.js b/elm/run_test.js index ce6abec..49282cc 100644 --- a/elm/run_test.js +++ b/elm/run_test.js @@ -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, }) diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index e78715c..cb9c868 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -51,7 +51,7 @@ rules_elm_integration_test_each_bazel( expected_output = """TEST RUN PASSED Duration: 0 ms -Passed: 3 +Passed: 4 Failed: 0""", ) diff --git a/examples/elm-test/BUILD.bazel b/examples/elm-test/BUILD.bazel index 4a812a8..9c06ef4 100644 --- a/examples/elm-test/BUILD.bazel +++ b/examples/elm-test/BUILD.bazel @@ -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", diff --git a/examples/elm-test/Main.elm b/examples/elm-test/Main.elm new file mode 100644 index 0000000..3d7465c --- /dev/null +++ b/examples/elm-test/Main.elm @@ -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 diff --git a/examples/elm-test/Spec.elm b/examples/elm-test/Spec.elm index ec1afe3..37d6913 100644 --- a/examples/elm-test/Spec.elm +++ b/examples/elm-test/Spec.elm @@ -3,6 +3,7 @@ module Spec exposing(..) import Expect import Test exposing (..) import Lib exposing(plus2) +import Main additionTests = describe "Addition" @@ -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 ] diff --git a/examples/elm-test/WORKSPACE b/examples/elm-test/WORKSPACE index be564e0..9905268 100644 --- a/examples/elm-test/WORKSPACE +++ b/examples/elm-test/WORKSPACE @@ -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", @@ -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"], )