Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change integration tests implementation to one based on rules_bazel_integration_test #1766

Merged
merged 21 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
86411c2
change integration tests implementation to rules_bazel_integration_te…
Jun 23, 2022
89d190f
implement haskell_module integration tests as
Jun 24, 2022
09ab1a8
make buildifier happy
Jun 24, 2022
0bab548
enable fail_not_supported for nixpkgs_package bazel packages for
Jun 24, 2022
aa139a5
added clang dependency in shell.nix required by
Jun 24, 2022
4fa9f49
use "is_nix_shell" to generate different targets in
Jun 24, 2022
3c3cfaf
pass BAZEL_USE_CPP_ONLY_TOOLCHAIN environment variable into integration
Jun 24, 2022
c45459c
don't run c2hs dependent integration test on macos-bindist an windows
Jun 24, 2022
03df6f6
make a list of bazel subcommands suitable for applying --config with
Jun 28, 2022
2d2d62b
added signatures for helper functions in IntegrationTesting
Jun 28, 2022
61ee958
add "dont_test_on_windows" to default tags for integration tests
Jun 28, 2022
8f23668
add signatures for global functions in IntegrationTesting
Jun 28, 2022
5e16530
add README for integration testing
Jun 28, 2022
56eaac4
prevent rules_haskell_integration_test expanding on windows
Jun 28, 2022
cb6b036
remove redundant comment
Jun 29, 2022
3b80768
typos
aherrmann Jul 1, 2022
23cfbed
set TMPDIR="/tmp" cause usual nix-shell tmp has not enough space to
Jul 5, 2022
04f752c
* Change bazel-contrib/rules_bazel_integration_test dependency to a …
Jul 5, 2022
8b50dd6
integration testing: pass nixpkgs info through env instead of args since
Jul 5, 2022
44ff83b
Wrap integration tests in hspec to improve readability of test outputs
Jul 7, 2022
90a9ab5
add hspec usage in example in integration testing README
Jul 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ build: --incompatible_override_toolchain_transition

coverage --build_tag_filters "coverage-compatible" --test_tag_filters "coverage-compatible" --test_output=all

# To update these lines, execute
# `bazel run @contrib_rules_bazel_integration_test//tools:update_deleted_packages`
k1nkreet marked this conversation as resolved.
Show resolved Hide resolved
build --deleted_packages=examples,examples/arm,examples/cat_hs,examples/cat_hs/exec/cat_hs,examples/cat_hs/lib/args,examples/cat_hs/lib/cat,examples/primitive,examples/rts,examples/transformers,examples/vector,tests/c2hs/repo,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-a,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-b,tests/haskell_module/repl/haskell_module_repl_test,tests/library-external-workspace/repo,tests/repl-targets/hs_bin_repl_test,tests/repl-targets/hs_lib_repl_test,tests/stack-snapshot-deps/hs_override_stack_test,tutorial,tutorial/lib,tutorial/main,tutorial/tools/build_rules
query --deleted_packages=examples,examples/arm,examples/cat_hs,examples/cat_hs/exec/cat_hs,examples/cat_hs/lib/args,examples/cat_hs/lib/cat,examples/primitive,examples/rts,examples/transformers,examples/vector,tests/c2hs/repo,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-a,tests/haskell_module/repl/haskell_module_repl_cross_library_deps_test/package-b,tests/haskell_module/repl/haskell_module_repl_test,tests/library-external-workspace/repo,tests/repl-targets/hs_bin_repl_test,tests/repl-targets/hs_lib_repl_test,tests/stack-snapshot-deps/hs_override_stack_test,tutorial,tutorial/lib,tutorial/main,tutorial/tools/build_rules


# User Configuration
# ------------------
try-import %workspace%/.bazelrc.local
1 change: 1 addition & 0 deletions BUILD.bazel
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ filegroup(
name = "distribution",
testonly = True,
srcs = [
".bazelrc",
"BUILD.bazel",
"WORKSPACE",
"constants.bzl",
Expand Down
35 changes: 24 additions & 11 deletions WORKSPACE
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -532,16 +532,8 @@ register_toolchains(
# For buildifier

# starting from 0.29, rules_go requires bazel >= 4.2.0
# rules_go dependency used only for rules_haskell developers for integration testing
# and it doesn't needed for downstream users of rules_haskell
# this patch is needed in order to use bazel integration testing mechanism from rules_go:
# 1. it fixes the issue with go_bazel_test on Windows: https://github.com/bazelbuild/rules_go/issues/3034
# fix will be available in rules_go starting from 0.30.0
# 2. it makes outputUserRoot variable public and available for commands running bazel functions redefined in rules_haskell
http_archive(
name = "io_bazel_rules_go",
patch_args = ["-p1"],
patches = ["//:rules_go_integration_testing.patch"],
sha256 = "8e968b5fcea1d2d64071872b12737bbb5514524ee5f0a4f54f5920266c261acb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.28.0/rules_go-v0.28.0.zip",
Expand Down Expand Up @@ -578,6 +570,23 @@ load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_depen

buildifier_dependencies()

http_archive(
name = "contrib_rules_bazel_integration_test",
sha256 = "f80c4052df80e9099ed0f2f27ef4084604333566a7b028f524ceae6e5569b429",
strip_prefix = "rules_bazel_integration_test-7ee995a20bbaa2f6540103c63ff4891166133c2f",
urls = [
"https://github.com/bazel-contrib/rules_bazel_integration_test/archive/7ee995a20bbaa2f6540103c63ff4891166133c2f.zip",
],
)

load("@contrib_rules_bazel_integration_test//bazel_integration_test:deps.bzl", "bazel_integration_test_rules_dependencies")

bazel_integration_test_rules_dependencies()

load("@cgrindel_bazel_starlib//:deps.bzl", "bazel_starlib_dependencies")

bazel_starlib_dependencies()

# For profiling
# Required to make use of `bazel build --profile`.

Expand All @@ -590,12 +599,9 @@ bind(

# For persistent worker (tools/worker)
load("//tools:repositories.bzl", "rules_haskell_worker_dependencies")
load("//tools:repositories.bzl", "bazel_binaries_for_integration_testing")

rules_haskell_worker_dependencies()

bazel_binaries_for_integration_testing()

# Stack snapshot repository for testing non standard toolchains
# The toolchain_libraries rule provide a default value for the toolchain_libraries
# variable, so we can load it even if we are not on linux.
Expand All @@ -617,6 +623,13 @@ stack_snapshot(
toolchain_libraries = toolchain_libraries,
) if is_linux else None

load(
"//tests/integration_testing:dependencies.bzl",
"integration_testing_bazel_binaries",
)

integration_testing_bazel_binaries()

local_repository(
name = "tutorial",
path = "tutorial",
Expand Down
7 changes: 7 additions & 0 deletions bazel_versions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SUPPORTED_BAZEL_VERSIONS = [
"4.1.0",
]

SUPPORTED_NIXPKGS_BAZEL_PACKAGES = [
"bazel_4",
]
47 changes: 0 additions & 47 deletions rules_go_integration_testing.patch

This file was deleted.

31 changes: 1 addition & 30 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ haskell_binary(
"//tests/hackage:directory",
"//tests/hackage:filepath",
"//tests/hackage:process",
"//tests/integration_testing",
"@stackage//:hspec",
"@stackage//:hspec-core",
"@stackage//:safe-exceptions",
Expand Down Expand Up @@ -425,36 +426,6 @@ haskell_repl(
],
)

filegroup(
name = "bazel_bindist",
srcs = select({
"@platforms//os:osx": ["@bazel_bin_darwin//file"],
"@platforms//os:linux": ["@bazel_bin_linux//file"],
"@platforms//os:windows": ["@bazel_bin_windows//file"],
}),
)

filegroup(
name = "bazel",
testonly = True,
srcs = select({
"//tests:nix": ["@bazel_4//:bazel_bin"],
"//conditions:default": [":bazel_bindist"],
}),
visibility = ["//tests:__subpackages__"],
)

go_library(
name = "integration_testing",
srcs = ["integration_testing.go"],
importpath = "github.com/tweag/rules_haskell/tests/integration_testing",
visibility = ["//visibility:public"],
deps = [
"@io_bazel_rules_go//go/tools/bazel",
"@io_bazel_rules_go//go/tools/bazel_testing",
],
)

filegroup(
name = "all_files",
testonly = True,
Expand Down
53 changes: 3 additions & 50 deletions tests/RunTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import Control.Exception.Safe (bracket_)
import Data.Foldable (for_)
import Data.List (isInfixOf, sort)
import System.Directory (copyFile)
import System.Exit (ExitCode(..))
import System.FilePath ((</>))
import System.Info (os)
import System.IO.Temp (withSystemTempDirectory)

import qualified System.Process as Process
import Test.Hspec.Core.Spec (SpecM)
import Test.Hspec (context, hspec, it, describe, runIO, shouldSatisfy, expectationFailure)
import Test.Hspec (context, hspec, it, describe, runIO)

import IntegrationTesting

main :: IO ()
main = hspec $ do
Expand Down Expand Up @@ -155,51 +156,3 @@ bazel args = Process.proc "bazel" args
-- | Runs a bazel query and return the list of matching targets
bazelQuery :: String -> SpecM a [String]
bazelQuery q = lines <$> runIO (Process.readProcess "bazel" ["query", q] "")

-- * Action helpers

-- | Ensure that @(stdout, stderr)@ of the command satisfies a predicate
outputSatisfy
:: ((String, String) -> Bool)
-> Process.CreateProcess
-> IO ()
outputSatisfy predicate cmd = do
(exitCode, stdout, stderr) <- Process.readCreateProcessWithExitCode cmd ""

case exitCode of
ExitSuccess -> (stdout, stderr) `shouldSatisfy` predicate
ExitFailure _ -> expectationFailure (formatOutput exitCode stdout stderr)

-- | The command must succeed
assertSuccess :: Process.CreateProcess -> IO ()
assertSuccess = outputSatisfy (const True)

-- | The command must fail
assertFailure :: Process.CreateProcess -> IO ()
assertFailure cmd = do
(exitCode, stdout, stderr) <- Process.readCreateProcessWithExitCode cmd ""

case exitCode of
ExitFailure _ -> pure ()
ExitSuccess -> expectationFailure ("Unexpected success of a failure test with output:\n" ++ formatOutput exitCode stdout stderr)

-- * Formatting helpers

formatOutput :: ExitCode -> String -> String -> String
formatOutput exitcode stdout stderr =
let
header = replicate 20 '-'
headerLarge = replicate 20 '='

in unlines [
headerLarge
, "Exit Code: " <> show exitcode
, headerLarge
, "Standard Output"
, header
, stdout
, headerLarge
, "Error Output"
, header
, stderr
, header]
12 changes: 7 additions & 5 deletions tests/haskell_module/repl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
load("//tests:integration_tests.bzl", "integration_test")
load("//tests/integration_testing:rules_haskell_integration_test.bzl", "rules_haskell_integration_test")

package(default_testonly = 1)

integration_test(
rules_haskell_integration_test(
name = "haskell_module_repl_test",
size = "small",
bazel = "//tests:bazel",
srcs = ["HaskellModuleReplTest.hs"],
tags = [
# See https://github.com/tweag/rules_haskell/issues/1486
"dont_test_on_darwin_with_bindist",
"dont_test_on_windows",
],
workspace_path = "haskell_module_repl_test",
)

integration_test(
rules_haskell_integration_test(
name = "haskell_module_repl_cross_library_deps_test",
size = "small",
bazel = "//tests:bazel",
srcs = ["HaskellModuleReplCrossLibraryDepsTest.hs"],
tags = [
# See https://github.com/tweag/rules_haskell/issues/1486
"dont_test_on_darwin_with_bindist",
"dont_test_on_windows",
],
workspace_path = "haskell_module_repl_cross_library_deps_test",
)

filegroup(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{-# OPTIONS -Wall #-}

import Test.Hspec (hspec, it)
import IntegrationTesting

main :: IO ()
main = hspec $ do
it "bazel run repl" $ do
bazel <- setupTestBazel
let p (stdout, _stderr) = lines stdout == ["42"]
in
outputSatisfy p (bazel ["run", "//package-b:package-b@repl", "--", "-ignore-dot-ghci", "-e", "mod1num"])
12 changes: 12 additions & 0 deletions tests/haskell_module/repl/HaskellModuleReplTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{-# OPTIONS -Wall #-}

import Test.Hspec (hspec, it)
import IntegrationTesting

main :: IO ()
main = hspec $ do
it "bazel run repl" $ do
bazel <- setupTestBazel
let p (stdout, _stderr) = lines stdout == ["420"]
in
outputSatisfy p (bazel ["run", "//:repl", "--", "-ignore-dot-ghci", "-e", "leaf"])
Loading