Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit 514911e

Browse files
authored
chore: add Makefile and docs around our dev/test processes (xenial) (#633)
* chore(tests): decouple bats tests from nvm.sh * chore(tests): add a Makefile * chore(docs): add docs for our test and dev processes * chore(docs): remove bullet point from linting section
1 parent 578d9f0 commit 514911e

File tree

7 files changed

+86
-5
lines changed

7 files changed

+86
-5
lines changed

CONTRIBUTING.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ please read the [code of conduct](CODE_OF_CONDUCT.md).
99
2. Wait for the Jenkins build to finish
1010
3. Create a PR in the [buildbot](https://github.com/netlify/buildbot) to bump [the version](https://github.com/netlify/buildbot/blob/a247edab7ead955cc27bb70ecc9f081e68f1aea6/script/docker-build.sh#L17) of the `build-image`.
1111

12+
## Development
13+
14+
### Linting
15+
16+
[ShellCheck](https://github.com/koalaman/shellcheck) usage is recommended, however it is not enforced.
17+
18+
### Tests
19+
20+
We have a set of automated tests in [./tests](./tests). These are [bats](https://github.com/bats-core/bats-core) tests that we use to make assertions not only on the correct functioning of our bash/shell scripts, but also of the software provided by our Docker image. Any fix or feature should be accompanied by a set of tests to validate that those changes work as expected. For an overview on how [bats works see here](https://bats-core.readthedocs.io/en/stable/).
21+
22+
### Developing
23+
24+
We provide a [Makefile](./Makefile) with a set of utility targets to help with development.
25+
26+
Some examples:
27+
28+
- `make test` build the test docker image and run the tests in [TAP fromat](http://testanything.org/).
29+
- `make test-local` creates a volume of the tests directory and the build scripts and run the tests inside the container. Useful when developing locally.
30+
- `make test-local FILTER=<regex>` provide a filter regex string to your test execution in order to select a specific set of tests.
31+
- `make run` build the base image and run a bash shell in a container based in it in interactive mode.
32+
- `make run-local` volume the build scripts and run a bash shell in a container based in the build image.
33+
1234
## License
1335

1436
By contributing to Netlify's build-image, you agree that your contributions will be licensed

Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,11 @@ ADD --chown=buildbot package.json /opt/buildhome/test-env/package.json
517517
RUN cd /opt/buildhome/test-env && . ~/.nvm/nvm.sh && npm i &&\
518518
ln -s /opt/build-bin/run-build-functions.sh /opt/buildhome/test-env/run-build-functions.sh &&\
519519
ln -s /opt/build-bin/build /opt/buildhome/test-env/run-build.sh
520+
520521
ADD --chown=buildbot tests /opt/buildhome/test-env/tests
521522
WORKDIR /opt/buildhome/test-env
522-
CMD . ~/.nvm/nvm.sh && npm test
523+
524+
# Set `bats` as entrypoint
525+
ENTRYPOINT ["node_modules/.bin/bats"]
526+
# Set the default flags for `bats`
527+
CMD ["--recursive", "--timing", "--tap", "tests"]

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
.PHONY: test test-local build-tests build-base run run-local
2+
3+
image = build-image
4+
test-image = $(image)-test
5+
6+
test: build-tests ## Run tests.
7+
docker run --rm -it \
8+
$(test-image)
9+
10+
11+
# TODO conditionally build the `$(test-image)` if it doesn't exist
12+
test-local: ## Volume the tests directory and build scripts, and run the tests. Useful while developing locally
13+
docker run --rm -it \
14+
-v $(PWD)/tests:/opt/buildhome/test-env/tests:ro \
15+
-v $(PWD)/run-build.sh:/opt/build-bin/build:ro \
16+
-v $(PWD)/run-build-functions.sh:/opt/build-bin/run-build-functions.sh:ro \
17+
$(test-image) \
18+
--filter "$(FILTER)" --recursive --timing --pretty tests
19+
20+
build-tests: ## Build test image, which includes all the test dependencies and tooling
21+
docker build \
22+
-t $(test-image) \
23+
.
24+
25+
build-base: ## Build base build-image docker image, without test dependencies and tooling
26+
docker build \
27+
-t $(image) \
28+
--target build-image \
29+
.
30+
31+
run: build-base ## Run a bash shell in the build-image
32+
docker run --rm -it \
33+
$(image)
34+
35+
# TODO conditionally build `$(image)` if it doesn't exist
36+
run-local: ## Volume the build scripts and run a bash shell in the build-image
37+
docker run --rm -it \
38+
-v $(PWD)/run-build.sh:/opt/build-bin/build:ro \
39+
-v $(PWD)/run-build-functions.sh:/opt/build-bin/run-build-functions.sh:ro \
40+
$(image)

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ If you're having problems with your build, you can also use these tools to test
66

77
## Available images
88

9-
Netlify maintains multiple build images for testing new development as well as supporting legacy builds. Each image uses a different version of Ubuntu Linux, with a slightly different list of included language and software versions.
9+
Netlify maintains multiple build images for testing new development as well as supporting legacy builds. Each image uses a different version of Ubuntu Linux, with a slightly different list of included language and software versions.
1010

1111
The following images are currently available:
1212

@@ -84,6 +84,10 @@ This will create a `tmp` directory that will have the repo that the buildbot clo
8484
T=tmp/tmp.XXXXX ./test-tools/test-build.sh path/to/site/repo 'your build command'
8585
```
8686

87+
## Development
88+
89+
Visit the [development section in our CONTRIBUTING.md](CONTRIBUTING.md#development)
90+
8791
## Contributing
8892

8993
Pull requests welcome, as long as they're not overly specific to a niche use-case.

tests/helpers.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ set_fixture_as_repo() {
2424
setup_tmp_dir() {
2525
mktemp -d
2626
}
27+
28+
# Sources nvm.sh, in order for node and npm binaries to be accessible
29+
source_nvm() {
30+
# Disable shellcheck's source file check
31+
# shellcheck source=/dev/null
32+
source ~/.nvm/nvm.sh
33+
}

tests/node/base.bats

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ load '../../node_modules/bats-support/load'
66
load '../../node_modules/bats-assert/load'
77

88

9-
NODE_VERSION=12.18.0
10-
11-
#Note: These binaries are accessible because we source `~/.nvm/nvm.sh` before running the `bats` tests
9+
setup() {
10+
source_nvm
11+
}
1212

1313
@test 'node version ${NODE_VERSION} is installed and available at startup' {
14+
NODE_VERSION=12.18.0
1415
run node --version
1516
assert_success
1617
assert_output --partial $NODE_VERSION

tests/node/yarn.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ setup() {
1414

1515
# Load functions
1616
load '../../run-build-functions.sh'
17+
18+
source_nvm
1719
}
1820

1921
teardown() {

0 commit comments

Comments
 (0)