Skip to content

Latest commit

 

History

History
159 lines (104 loc) · 2.99 KB

tutorial.md

File metadata and controls

159 lines (104 loc) · 2.99 KB

Tutorial

Go from an empty directory to a passing dx test. The steps are for Linux.

Install

rules_dx is not released yet. Install starts with the first BCR release.

Install Bazelisk, then install dx from the latest release.

curl -fsSL -o bazel https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64
chmod +x bazel
gh release download --repo devhusdk/rules_dx --pattern 'dx-linux-x86_64.tar.gz' --dir dx-release
mkdir -p ~/.local/bin
tar -xzf dx-release/dx-linux-x86_64.tar.gz -C ~/.local/bin dx

Put bazel and ~/.local/bin on your PATH and open a new shell.

Start a workspace

dx init --apply my-app
cd my-app

A Bazel workspace is a directory with a MODULE.bazel. A module is the unit Bazel builds and other modules depend on. dx init writes the file for you.

Add a hello package

A package is a directory with a BUILD.bazel. Make one called hello.

mkdir hello

Create hello/hello.py.

"""Greeting helper."""


def greet(name):
    """Return a greeting for name."""
    return f"Hello, {name}!"

Create hello/hello_test.py.

"""Tests for the greeting helper."""

import unittest

from hello.hello import greet


class GreetTest(unittest.TestCase):
    def test_greet(self):
        self.assertEqual(greet("world"), "Hello, world!")


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

Let dx write the BUILD.bazel.

dx generate --apply

hello/BUILD.bazel now lists targets. A target is one thing Bazel can build or test. A label names it, like //hello:hello_test: the package path after //, then the target name after :. //... means every target in the workspace.

Build and test

dx build //...
dx test //...

Fix problems

dx check --apply //...

Add a second package

Make a greeter package that depends on hello.

mkdir greeter

Create greeter/greeter.py.

"""Greets everyone."""

from hello.hello import greet


def greet_all(names):
    """Return one greeting per name."""
    return [greet(name) for name in names]

Create greeter/greeter_test.py.

"""Tests for greet_all."""

import unittest

from greeter.greeter import greet_all


class GreeterTest(unittest.TestCase):
    def test_greet_all(self):
        self.assertEqual(greet_all(["a"]), ["Hello, a!"])


if __name__ == "__main__":
    unittest.main()
dx generate --apply
dx test //...

Test only what changed

Commit your work, change hello/hello.py, then run only the affected tests. Both packages run, because greeter depends on hello.

git init
git add .
git commit -m "hello"
dx test --affected

CI

Run the same checks on every pull request. See GitHub CI.

Next