Skip to content

Commit 63d5bf8

Browse files
authored
Merge pull request github#331 from xenoscopic/unittest-enable
test: enable unit tests and make validate tests flexible in terms of working directory
2 parents 3921f08 + 0c898eb commit 63d5bf8

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ on:
22
pull_request:
33

44
jobs:
5-
validate-servers:
5+
validate:
6+
name: "Code and Server Validation"
67
runs-on: ubuntu-latest
78
steps:
89
- uses: actions/checkout@v4
@@ -21,6 +22,9 @@ jobs:
2122
version: 3.x
2223
repo-token: ${{ secrets.GITHUB_TOKEN }}
2324

25+
- name: Run unit tests
26+
run: task unittest
27+
2428
- name: Get changed servers
2529
shell: bash
2630
run: |

Taskfile.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ tasks:
3434
cmds:
3535
- docker mcp catalog reset
3636
- docker mcp catalog init
37+
38+
unittest:
39+
desc: Run Go unit tests
40+
cmd: go test ./...

cmd/validate/main_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
package main
22

33
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"runtime"
48
"testing"
59
)
610

11+
func TestMain(m *testing.M) {
12+
// Compute the path to this source code file.
13+
_, thisFile, _, ok := runtime.Caller(0)
14+
if !ok {
15+
fmt.Fprintln(os.Stderr, "mcp-registry/cmd/validate: unable to resolve caller path")
16+
os.Exit(1)
17+
}
18+
19+
// Switch to the repository root so that readServerYaml calls from tests can
20+
// access YAML files.
21+
repoRoot := filepath.Clean(filepath.Join(filepath.Dir(thisFile), "..", ".."))
22+
if err := os.Chdir(repoRoot); err != nil {
23+
fmt.Fprintln(os.Stderr, "mcp-registry/cmd/validate: chdir:", err)
24+
os.Exit(1)
25+
}
26+
27+
// Run the tests in this package.
28+
code := m.Run()
29+
30+
// Restore the working directory.
31+
originalWD := filepath.Clean(filepath.Join(repoRoot, "cmd", "validate"))
32+
if err := os.Chdir(originalWD); err != nil {
33+
fmt.Fprintln(os.Stderr, "mcp-registry/cmd/validate: restore chdir:", err)
34+
os.Exit(1)
35+
}
36+
37+
os.Exit(code)
38+
}
39+
740
func Test_isNameValid(t *testing.T) {
841
type args struct {
942
name string

0 commit comments

Comments
 (0)