From f9dfb4dff4d8edd3ef85f58e0eb8f6783d7a5495 Mon Sep 17 00:00:00 2001 From: w0rp Date: Mon, 7 Jul 2025 20:09:37 +0100 Subject: [PATCH 1/3] Add tests in CI for the project --- .github/workflows/ci.yml | 41 ++++++++++++++++++++++++++++ .yamllint.yml | 5 ++++ cmd/sea/main_test.go | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .yamllint.yml create mode 100644 cmd/sea/main_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c3edb5f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,41 @@ +--- +name: CI + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + env: + NGINX_VERSION: latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.22' + - name: Go Tests + run: go test ./... + - name: Cache nginx image + id: cache-nginx + uses: actions/cache@v3 + with: + path: ~/.cache/nginx.tar + key: ${{ runner.os }}-nginx-${{ env.NGINX_VERSION }} + - name: Load nginx image from cache + if: steps.cache-nginx.outputs.cache-hit == 'true' + run: docker load -i ~/.cache/nginx.tar + - name: Pull nginx image + if: steps.cache-nginx.outputs.cache-hit != 'true' + run: | + docker pull "nginx:$NGINX_VERSION" + mkdir -p ~/.cache + docker save "nginx:$NGINX_VERSION" -o ~/.cache/nginx.tar + - name: Validate generated nginx config + run: | + go run ./cmd/sea/main.go > nginx.conf + docker run --rm \ + -v "$PWD/nginx.conf:/etc/nginx/conf.d/default.conf:ro" \ + "nginx:$NGINX_VERSION" \ + nginx -t diff --git a/.yamllint.yml b/.yamllint.yml new file mode 100644 index 0000000..c4e78e2 --- /dev/null +++ b/.yamllint.yml @@ -0,0 +1,5 @@ +--- +extends: default +rules: + # Allow `foo: "on"` and other commonly used values. + truthy: disable diff --git a/cmd/sea/main_test.go b/cmd/sea/main_test.go new file mode 100644 index 0000000..e157e1b --- /dev/null +++ b/cmd/sea/main_test.go @@ -0,0 +1,59 @@ +package main + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestEscapeSpace(t *testing.T) { + got := escapeSpace("hello world") + if got != "hello\\ world" { + t.Errorf("expected 'hello\\\\ world', got %q", got) + } +} + +func TestLoadConfig(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "cfg.toml") + data := []byte(`listen = 8080 +server_name = "example.com" + +[[custom_keywords]] +phrase = "foo bar" +dest = "google"`) + if err := os.WriteFile(path, data, 0644); err != nil { + t.Fatalf("failed to write config: %v", err) + } + cfg, err := loadConfig(path) + if err != nil { + t.Fatalf("loadConfig returned error: %v", err) + } + if cfg.Listen != 8080 { + t.Errorf("expected listen 8080, got %d", cfg.Listen) + } + if cfg.ServerName != "example.com" { + t.Errorf("expected server name example.com, got %s", cfg.ServerName) + } + if len(cfg.CustomKeywords) != 1 { + t.Fatalf("expected 1 custom keyword, got %d", len(cfg.CustomKeywords)) + } + if cfg.CustomKeywords[0].Phrase != "foo bar" || cfg.CustomKeywords[0].Dest != "google" { + t.Errorf("unexpected custom keyword %+v", cfg.CustomKeywords[0]) + } +} + +func TestGenerateNginx(t *testing.T) { + cfg := Config{Listen: 8080, ServerName: "example.com", CustomKeywords: []KeywordRule{{Phrase: "foo", Dest: "google"}}} + out, err := generateNginx(cfg) + if err != nil { + t.Fatalf("generateNginx returned error: %v", err) + } + if !strings.Contains(out, "server_name example.com;") { + t.Errorf("generated config missing server name: %s", out) + } + if !strings.Contains(out, "~*(?i)^foo$") { + t.Errorf("generated config missing custom rule: %s", out) + } +} From 2f58a3c47d3b368a775e4c7290c1969137d9e86f Mon Sep 17 00:00:00 2001 From: w0rp Date: Mon, 7 Jul 2025 20:11:09 +0100 Subject: [PATCH 2/3] Make CI checks run less --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c3edb5f..06825af 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,8 +2,10 @@ name: CI on: - push: pull_request: + push: + branches: + - main jobs: build: From de308af8155c7c701ca39cddc9a2cdc43b08c154 Mon Sep 17 00:00:00 2001 From: w0rp Date: Mon, 7 Jul 2025 20:15:32 +0100 Subject: [PATCH 3/3] Tell agents how to run tests --- AGENTS.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index f3eb752..e27364a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -5,7 +5,7 @@ to search providers based on the phrases used. You can run the main executable like so: -``` +```sh go run ./cmd/sea/main.go ``` @@ -14,10 +14,16 @@ go run ./cmd/sea/main.go to get it. Commit `config.toml` as a default configuration people can override if they so choose. +You can run Go tests like so: + +```sh +go test ./... +``` + You may validate that the nginx configuration works with the docker compose file like so: -``` +```sh # Leave an nginx docker image running on port 57321 docker compose up # Check a redirect. The hostname comes from `server_name` in `config.toml`