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

chore: Run tests in parallel jobs #4076

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 34 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ jobs:
export DOCKER_GOCACHE="$HOME/.go-archlinux"
./assets/docker/test.sh archlinux
test-macos:
name: test-macos-${{ matrix.test-index }}
strategy:
fail-fast: false
matrix:
test-index: [0, 1, 2]
needs: changes
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
runs-on: macos-15
Expand Down Expand Up @@ -159,7 +164,14 @@ jobs:
env:
CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
run: |
go test -race ./...
if [ "${{ matrix.test-index }}" = "0" ]; then
go test ./... -short -race
go test ./internal/cmd -run=TestScript -filter='^[0-9a-dA-D]' -race
elif [ "${{ matrix.test-index }}" = "1" ]; then
go test ./internal/cmd -run=TestScript -filter='^[e-iE-I]' -race
else
go test ./internal/cmd -run=TestScript -filter='^[j-zJ-Z]' -race
fi
test-oldstable-go:
needs: changes
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
Expand Down Expand Up @@ -257,13 +269,14 @@ jobs:
name: chezmoi-windows-amd64
path: dist/chezmoi-nocgo_windows_amd64_v1/chezmoi.exe
test-ubuntu:
name: test-ubuntu-umask${{ matrix.umask }}
name: test-ubuntu-umask${{ matrix.umask }}-${{ matrix.test-index }}
strategy:
fail-fast: false
matrix:
umask:
- "022"
- "002"
test-index: [0, 1]
needs: changes
runs-on: ubuntu-22.04
permissions:
Expand Down Expand Up @@ -298,8 +311,14 @@ jobs:
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
env:
CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
run: |
go test -ldflags="-X github.com/twpayne/chezmoi/v2/internal/chezmoitest.umaskStr=0o${{ matrix.umask }}" -race -timeout=1h ./...
TEST_FLAGS: '-ldflags="-X github.com/twpayne/chezmoi/v2/internal/chezmoitest.umaskStr=0o${{ matrix.umask }}" -race -timeout=1h'
run: |
if [ "${{ matrix.test-index }}" = "0" ]; then
go test ./... -short ${{ env.TEST_FLAGS }}
go test ./internal/cmd -run=TestScript -filter='^[0-9a-hA-h]' ${{ env.TEST_FLAGS }}
else
go test ./internal/cmd -run=TestScript -filter='^[i-zI-Z]' ${{ env.TEST_FLAGS }}
fi
test-website:
runs-on: ubuntu-22.04
permissions:
Expand All @@ -323,6 +342,11 @@ jobs:
env:
CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
test-windows:
name: test-windows-${{ matrix.test-index }}
strategy:
fail-fast: false
matrix:
test-index: [0, 1]
needs: changes
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
runs-on: windows-2022
Expand All @@ -343,7 +367,12 @@ jobs:
env:
CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
run: |
go test -race ./...
if (${{ matrix.test-index }} -eq 0) {
go test ./... -short -race
go test ./internal/cmd -run=TestScript -filter='^[0-9a-hA-h]' -race
} else {
go test ./internal/cmd -run=TestScript -filter='^[i-zI-Z]' -race
}
check:
runs-on: ubuntu-22.04
permissions:
Expand Down
33 changes: 32 additions & 1 deletion internal/cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
_ "embed"
"errors"
"flag"
"fmt"
"io/fs"
"net"
Expand Down Expand Up @@ -37,6 +38,8 @@ var (
lookupRx = regexp.MustCompile(`\Alookup:(.*)\z`)
umaskConditionRx = regexp.MustCompile(`\Aumask:([0-7]{3})\z`)

filterRegex string

//go:embed mockcommand.tmpl
mockcommandTmplText string

Expand All @@ -45,6 +48,10 @@ var (
)

func TestMain(m *testing.M) {
if strings.Contains(os.Args[0], "cmd.test") {
flag.StringVar(&filterRegex, "filter", "", "regex to filter test scripts")
flag.Parse()
}
os.Exit(testscript.RunMain(m, map[string]func() int{
"chezmoi": func() int {
return cmd.Main(cmd.VersionInfo{
Expand All @@ -58,8 +65,32 @@ func TestMain(m *testing.M) {
}

func TestScript(t *testing.T) {
if testing.Short() {
t.Skip("skipping testscript tests in short mode")
}
files, err := filepath.Glob("testdata/scripts/*.txtar")
if err != nil {
t.Fatalf("failed to glob files: %v", err)
}
if filterRegex != "" {
re, err := regexp.Compile(filterRegex)
if err != nil {
t.Fatalf("invalid regex %q: %v", filterRegex, err)
}
var filteredFiles []string
for _, f := range files {
baseName := strings.Split(filepath.Base(f), ".")[0]
if re.MatchString(baseName) {
filteredFiles = append(filteredFiles, f)
}
}
files = filteredFiles
if len(files) == 0 {
t.Fatalf("no test scripts match regex %q", filterRegex)
}
}
testscript.Run(t, testscript.Params{
Dir: filepath.Join("testdata", "scripts"),
Files: files,
Cmds: map[string]func(*testscript.TestScript, bool, []string){
"appendline": cmdAppendLine,
"chhome": cmdChHome,
Expand Down
Loading