Skip to content

Commit

Permalink
Add Buildkite config and tests for Bazelisk.
Browse files Browse the repository at this point in the history
  • Loading branch information
philwo authored Jan 18, 2019
2 parents b672790 + 8913826 commit 99d1b46
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
19 changes: 19 additions & 0 deletions .bazelci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
platforms:
ubuntu1404:
test_targets:
- //:bazelisk_test
ubuntu1604:
test_targets:
- //:bazelisk_test
ubuntu1804:
test_targets:
- //:bazelisk_test
macos:
test_targets:
- //:bazelisk_test
windows:
test_targets:
- //:bazelisk_test
test_flags:
- --test_env=PROCESSOR_ARCHITECTURE
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.formatting.provider": "yapf",
"python.linting.pylintEnabled": false,
}
10 changes: 10 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
py_library(
name = "bazelisk_lib",
srcs = ["bazelisk.py"],
)

py_test(
name = "bazelisk_test",
srcs = ["bazelisk_test.py"],
deps = [":bazelisk_lib"],
)
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

29 changes: 18 additions & 11 deletions bazelisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ def resolve_version_label_to_number(bazelisk_directory, version):
def determine_bazel_filename(version):
machine = normalized_machine_arch_name()
if machine != 'x86_64':
raise Exception('Unsupported machine architecture "{}". '
'Bazel currently only supports x86_64.'.format(machine))
raise Exception(
'Unsupported machine architecture "{}". Bazel currently only supports x86_64.'
.format(machine))

operating_system = platform.system().lower()
if operating_system not in ('linux', 'darwin', 'windows'):
Expand Down Expand Up @@ -147,11 +148,24 @@ def maybe_makedirs(path):
raise e


def execute_bazel(bazel_path, argv):
# We cannot use close_fds on Windows, so disable it there.
p = subprocess.Popen([bazel_path] + argv, close_fds=os.name != 'nt')
while True:
try:
return p.wait()
except KeyboardInterrupt:
# Bazel will also get the signal and terminate.
# We should continue waiting until it does so.
pass


def main(argv=None):
if argv is None:
argv = sys.argv

bazelisk_directory = os.path.join(os.path.expanduser('~'), '.bazelisk')
bazelisk_directory = os.environ.get(
"BAZELISK_HOME", os.path.join(os.path.expanduser('~'), '.bazelisk'))
maybe_makedirs(bazelisk_directory)

bazel_version = decide_which_bazel_version_to_use()
Expand All @@ -162,14 +176,7 @@ def main(argv=None):
maybe_makedirs(bazel_directory)
bazel_path = download_bazel_into_directory(bazel_version, bazel_directory)

p = subprocess.Popen([bazel_path] + argv[1:], close_fds=True)
while True:
try:
return p.wait()
except KeyboardInterrupt:
# Bazel will also get the signal and terminate.
# We should continue waiting until it does so.
pass
return execute_bazel(bazel_path, argv[1:])


if __name__ == '__main__':
Expand Down
51 changes: 51 additions & 0 deletions bazelisk_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
import os
import unittest
import subprocess
import tempfile

import bazelisk


def execute_bazel(bazel_path, argv):
return subprocess.check_output(
[bazel_path] + argv, close_fds=os.name != 'nt',
stderr=subprocess.STDOUT).decode("utf-8")


class TestBazelisk(unittest.TestCase):
"""Integration tests for Bazelisk."""

def setUp(self):
# Override Bazelisk's default function to execute Bazel, so that we can grab the output.
bazelisk.execute_bazel = execute_bazel
os.environ["BAZELISK_HOME"] = tempfile.mkdtemp(
dir=os.environ["TEST_TMPDIR"])

def test_bazel_version(self):
output = bazelisk.main(["bazelisk", "version"])
self.assertTrue("Build label" in output)

def test_bazel_version_from_environment(self):
os.environ["USE_BAZEL_VERSION"] = "0.20.0"
try:
output = bazelisk.main(["bazelisk", "version"])
finally:
del os.environ["USE_BAZEL_VERSION"]
self.assertTrue("Build label: 0.20.0" in output)

def test_bazel_version_from_file(self):
with open("WORKSPACE", "w") as f:
pass
with open(".bazelversion", "w") as f:
f.write("0.19.0")
try:
output = bazelisk.main(["bazelisk", "version"])
finally:
os.unlink("WORKSPACE")
os.unlink(".bazelversion")
self.assertTrue("Build label: 0.19.0" in output)


if __name__ == "__main__":
unittest.main()

0 comments on commit 99d1b46

Please sign in to comment.