|
| 1 | +# Contributing to resticprofile |
| 2 | + |
| 3 | +Thanks for your interest in contributing! This document explains how to set up |
| 4 | +your environment, build the project, and run the tests. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +- **Go** — version `1.26` or later (see [`.github/workflows/env`](.github/workflows/env) |
| 9 | + for the exact version used by CI). |
| 10 | +- **make** — all common tasks are wired through the [`Makefile`](Makefile). |
| 11 | +- **git** — the repository uses a submodule for the documentation theme. Clone with |
| 12 | + submodules, or initialise them after cloning: |
| 13 | + |
| 14 | + ```shell |
| 15 | + git clone --recurse-submodules https://github.com/creativeprojects/resticprofile.git |
| 16 | + # or, if already cloned: |
| 17 | + git submodule update --init --recursive |
| 18 | + ``` |
| 19 | + |
| 20 | +Optional tooling (downloaded automatically into `$GOBIN` by the relevant make |
| 21 | +targets, so you usually don't install these by hand): |
| 22 | + |
| 23 | +- [`gotestsum`](https://github.com/gotestyourself/gotestsum) — test runner used by the `test*` targets |
| 24 | +- [`mockery`](https://github.com/vektra/mockery) — regenerates mocks before tests |
| 25 | +- [`golangci-lint`](https://github.com/golangci/golangci-lint) — linter (pinned version, see `.github/workflows/env`) |
| 26 | +- [`restic`](https://restic.net/) — only needed if you want to run resticprofile against a real repository |
| 27 | + |
| 28 | +Run `make help` at any time to list all available targets. |
| 29 | + |
| 30 | +## Building |
| 31 | + |
| 32 | +Build the binary for your current platform: |
| 33 | + |
| 34 | +```shell |
| 35 | +make build |
| 36 | +``` |
| 37 | + |
| 38 | +This produces a `resticprofile` binary in the repository root. The target first |
| 39 | +runs `prepare_build` (verifies your Go installation and downloads module |
| 40 | +dependencies), then compiles with version metadata embedded via `-ldflags`. |
| 41 | + |
| 42 | +Other build targets: |
| 43 | + |
| 44 | +| Target | Description | |
| 45 | +| ----------------------- | ---------------------------------------------------------- | |
| 46 | +| `make install` | Build and install the binary into `$GOBIN` | |
| 47 | +| `make build-no-selfupdate` | Build without the self-update feature (`no_self_update` tag) | |
| 48 | +| `make build-mac` | Cross-compile for macOS (amd64 + arm64) | |
| 49 | +| `make build-linux` | Cross-compile for Linux (amd64 + arm64) | |
| 50 | +| `make build-windows` | Cross-compile for Windows (amd64 + arm64) | |
| 51 | +| `make build-all` | Cross-compile for all of the above | |
| 52 | + |
| 53 | +To remove build artifacts (binaries, coverage files, generated mocks, etc.): |
| 54 | + |
| 55 | +```shell |
| 56 | +make clean |
| 57 | +``` |
| 58 | + |
| 59 | +## Running the tests |
| 60 | + |
| 61 | +Run the full unit test suite: |
| 62 | + |
| 63 | +```shell |
| 64 | +make test |
| 65 | +``` |
| 66 | + |
| 67 | +The `test` target automatically: |
| 68 | + |
| 69 | +1. Installs `gotestsum` (into `$GOBIN`) if needed. |
| 70 | +2. Runs `prepare_test`, which regenerates mocks with `mockery`. |
| 71 | +3. Builds the test helper binaries under `testhelpers/` (`test-args`, |
| 72 | + `test-echo`, `test-crontab`, `test-shell`) and exposes their location via the |
| 73 | + `TEST_HELPERS` environment variable. |
| 74 | +4. Runs the tests with `gotestsum`. |
| 75 | + |
| 76 | +Useful variations: |
| 77 | + |
| 78 | +| Target | Description | |
| 79 | +| ----------------- | ------------------------------------------------------------- | |
| 80 | +| `make test-short` | Run tests in short mode (`-short`) | |
| 81 | +| `make test-race` | Run tests with the race detector (short mode) | |
| 82 | +| `make test-ci` | Run tests as CI does: race detector, `-short`, `fuse` build tag, coverage profile and JUnit report | |
| 83 | +| `make coverage` | Generate a coverage profile and open the HTML report | |
| 84 | + |
| 85 | +### Running a subset of tests |
| 86 | + |
| 87 | +The test targets honour the `TESTS` variable (default `./...`). For example, to |
| 88 | +run the tests of a single package: |
| 89 | + |
| 90 | +```shell |
| 91 | +make test TESTS=./config/... |
| 92 | +``` |
| 93 | + |
| 94 | +You can also run the standard Go tooling directly, but remember to point |
| 95 | +`TEST_HELPERS` at the directory containing the helper binaries built by |
| 96 | +`make test-helpers`: |
| 97 | + |
| 98 | +```shell |
| 99 | +make test-helpers |
| 100 | +TEST_HELPERS=$(pwd)/build/ go test ./config/... |
| 101 | +``` |
| 102 | + |
| 103 | +### FUSE tests |
| 104 | + |
| 105 | +Some tests are guarded behind the `fuse` build tag. They require FUSE |
| 106 | +support on your machine. |
| 107 | + |
| 108 | +### SSH client tests |
| 109 | + |
| 110 | +The SSH client integration tests need a containerised SSH server and are run |
| 111 | +separately: |
| 112 | + |
| 113 | +```shell |
| 114 | +make start-ssh-server # spins up the SSH server via docker compose |
| 115 | +make ssh-test # runs the SSH client tests (ssh build tag) |
| 116 | +make stop-ssh-server # tears the server down and cleans up |
| 117 | +``` |
| 118 | + |
| 119 | +These require Docker (with `docker compose`) and `ssh-keygen`. |
| 120 | + |
| 121 | +## Linting |
| 122 | + |
| 123 | +CI runs `golangci-lint`. To run it locally: |
| 124 | + |
| 125 | +```shell |
| 126 | +make lint # lint for darwin, linux and windows build targets |
| 127 | +make fix # run go mod tidy, go fix, and golangci-lint --fix |
| 128 | +``` |
| 129 | + |
| 130 | +The linter version is pinned in [`.github/workflows/env`](.github/workflows/env) |
| 131 | +and the configuration lives in [`.golangci.yml`](.golangci.yml). |
| 132 | + |
| 133 | +## Generated files |
| 134 | + |
| 135 | +Some files are generated and should be regenerated when you change their |
| 136 | +sources: |
| 137 | + |
| 138 | +- **Mocks** are regenerated automatically by `make prepare_test` (used by the |
| 139 | + test targets), based on [`.mockery.yml`](.mockery.yml). |
| 140 | +- **`go generate`** is run as part of `make test-ci`; you can also run |
| 141 | + `go generate ./...` directly. |
| 142 | +- **JSON schema** and the **configuration reference** are generated from the |
| 143 | + built binary with `make generate-jsonschema` and |
| 144 | + `make generate-config-reference`. The documentation site is built with |
| 145 | + `make documentation` (requires Hugo and the docs submodule). |
| 146 | + |
| 147 | +## Continuous integration |
| 148 | + |
| 149 | +Pull requests are validated on Linux, macOS, Windows and several BSDs. The |
| 150 | +Linux/macOS/Windows jobs run the shared workflow |
| 151 | +[`.github/workflows/run-tests-os.yml`](.github/workflows/run-tests-os.yml), |
| 152 | +which essentially performs: |
| 153 | + |
| 154 | +```shell |
| 155 | +make build |
| 156 | +make test-ci |
| 157 | +``` |
| 158 | + |
| 159 | +Before opening a pull request, it's a good idea to run at least: |
| 160 | + |
| 161 | +```shell |
| 162 | +make build |
| 163 | +make lint |
| 164 | +make test |
| 165 | +``` |
| 166 | + |
| 167 | +## Submitting changes |
| 168 | + |
| 169 | +1. Fork the repository and create a topic branch from `master`. |
| 170 | +2. Make your changes, keeping the existing code style and adding tests where it |
| 171 | + makes sense. |
| 172 | +3. Run `make build`, `make lint` and `make test` and make sure they pass. |
| 173 | +4. Open a pull request describing your change. |
| 174 | + |
| 175 | +Thanks for contributing! 🎉 |
0 commit comments