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

Implement hs-bin@repl test as go bazel test #1645

Merged
merged 29 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e85240c
add asterius files into "all_files" filegroup
Nov 29, 2021
90e8800
add go_bazel_test for hs-bin repl test
Nov 30, 2021
ffcda3e
remove hs-bin repl test from RunTest.hs
Nov 30, 2021
b67992b
add creation .bazelrc file with host_platform for integration test
Dec 6, 2021
5ce3afa
Fix bazel lint
Dec 6, 2021
16e75b3
add integration_test rule, passing nixpgs flag to go_bazel_test to
Dec 10, 2021
6bd8095
use //tests:nix config setting instead of creating new one
Dec 13, 2021
c71057c
set BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 when running bazel on darwin
Dec 13, 2021
4acea62
add windows configuration in bazelrc
Dec 13, 2021
b9d4373
add go library for common functions for integration tests implemented as
Dec 14, 2021
06562d3
fix bazel linting
Dec 14, 2021
47832af
add patch for rules fixes go_bazel_test on Windows:
Dec 19, 2021
2113761
fix bazel linter
Dec 19, 2021
d13b654
* added macro invocation pulling bazel binaries for all platforms as …
Dec 21, 2021
ffd89cc
use bazel.Runfile from rules_go to extract path to bazel binary
Dec 21, 2021
3661a87
add BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN for macos-nixpkgs setup
Jan 13, 2022
1f5aa2a
add --incompatible_enable_cc_toolchain_resolution for macos nixpkgs
Jan 13, 2022
be7de93
add --incompatible_enable_cc_toolchain_resolution flag for all
Jan 14, 2022
271f7d8
add TMPDIR=/tmp in mkShell according to
Jan 25, 2022
e95934a
clarify a comment regarding picking $HOME value for test
Jan 25, 2022
4b598b9
Merge branch 'master' into hsbin-as-go-bazel-test
Jan 25, 2022
7aba166
fix bazel linting
Jan 25, 2022
2d617f8
update haskell compiler version in integration tests: 8.10.4 -> 8.10.7
Jan 25, 2022
9a668e9
apply gofmt to go files
Jan 27, 2022
f0c9206
more clear comment on $HOME direcotry choice
Jan 28, 2022
fcd70e1
add comment describing patch for rules_go
Jan 28, 2022
9003072
use bazel from nixpkgs for integration testing in nix configurations:
Jan 29, 2022
f4655fb
added comment on rules_go dependency
Feb 4, 2022
5160178
Merge branch 'master' into hsbin-as-go-bazel-test
mergify[bot] Feb 8, 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
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test --test_env=LANG=C.UTF-8 --test_env=LOCALE_ARCHIVE
build:linux-nixpkgs --config=nixpkgs
build:macos-nixpkgs --config=nixpkgs
build:nixpkgs --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
test:nixpkgs --define=nixpkgs=true
# Use this configuration when targeting Windows. Eventually this will
# no longer be required:
# https://bazel.build/roadmaps/platforms.html#replace---cpu-and---host_cpu-flags.
Expand Down
22 changes: 22 additions & 0 deletions tests/integration_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")

def integration_test(name, **kwargs):
test_src = name + ".go"
size = kwargs.pop("size", "medium")

native.config_setting(
name = "nixpkgs",
values = {"define": "nixpkgs=true"},
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could reuse this config_setting based on the support_nix platform constraint. Then you also don't need to introduce the --define=nixpkgs above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed


go_bazel_test(
name = name,
srcs = [test_src],
size = size,
rule_files = ["//:distribution"],
args = select({
":nixpkgs": ["nixpkgs=true"],
"//conditions:default": ["nixpkgs=false"],
}),
**kwargs
)
5 changes: 2 additions & 3 deletions tests/repl-targets/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load(
"haskell_test",
)
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")
load("//tests:integration_tests.bzl", "integration_test")

package(default_testonly = 1)

Expand Down Expand Up @@ -113,11 +114,9 @@ haskell_library(
deps = ["//tests/hackage:base"],
)

go_bazel_test(
integration_test(
name = "hs_bin_repl_test",
size = "medium",
srcs = ["hs_bin_repl_test.go"],
rule_files = ["//:distribution"],
)

filegroup(
Expand Down
63 changes: 56 additions & 7 deletions tests/repl-targets/hs_bin_repl_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package hs_bin_repl_test

import (
"bytes"
"fmt"
"os"
"os/exec"
"testing"
"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{
Main: `


func Workspace() string {
return `
-- WORKSPACE --
local_repository(
name = "rules_haskell",
Expand Down Expand Up @@ -80,8 +85,6 @@ haskell_test(
],
)

-- .bazelrc --
build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
-- Quux.hs --
module Main (main) where

Expand All @@ -94,7 +97,12 @@ module QuuxLib (message) where

message :: String
message = "Hello GHCi!"
`,
`
}

func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{
Main: Workspace() + GenerateBazelrc(UseNixpkgs()),
})
}

Expand All @@ -104,8 +112,49 @@ func AssertOutput(t *testing.T, output []byte, expected string) {
}
}

func UseNixpkgs() bool {
for _, arg := range os.Args {
if arg == "nixpkgs=true" {
return true
}
}
return false
}

func GenerateBazelrc(use_nixpkgs bool) string {
if use_nixpkgs {
return `
-- .bazelrc --
build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
`
} else {
return ""
}
}

func BazelOutput(args ...string) ([]byte, error) {
cmd := bazel_testing.BazelCmd(args...)
// It's important value of $HOME to be invariant between different integration test runs
// and to be writable directory for bazel test. Probably TEST_TMPDIR is a valid choice
// but documentation is not clear about it's default value
// cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", os.Getenv("TEST_TMPDIR")))
cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", os.TempDir()))
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = stdout
cmd.Stderr = stderr
err := cmd.Run()
if eErr, ok := err.(*exec.ExitError); ok {
eErr.Stderr = stderr.Bytes()
err = &bazel_testing.StderrExitError{Err: eErr}
}
fmt.Fprintf(os.Stderr, string(stdout.Bytes()))
fmt.Fprintf(os.Stderr, string(stderr.Bytes()))
return stdout.Bytes(), err
}

func TestHsBinRepl(t *testing.T) {
out, err := bazel_testing.BazelOutput("run", "//:hs-bin@repl", "--", "-ignore-dot-ghci", "-e", ":main")
out, err := BazelOutput("run", "//:hs-bin@repl", "--", "-ignore-dot-ghci", "-e", ":main")
if err != nil {
t.Fatal(err)
}
Expand Down