Skip to content
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
3 changes: 1 addition & 2 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"$schema": "https://biomejs.dev/schemas/2.6.5/schema.json",
"root": true,
"extends": ["@rigelbuild/biome-config/biome"],
"extends": ["./packages/biome-config/biome.json"],
"files": {
"includes": [
"**",
"!rumdl-extends.test.ts",
"!go/commentscan/testdata",
"!go/ledger/testdata",
"!**/CHANGELOG.md"
Expand Down
112 changes: 112 additions & 0 deletions go/internal/repochecks/package_bins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

package repochecks

import (
"os"
"path/filepath"
"strings"
"testing"
)

// declaredBins returns the bin paths a package.json record declares, as a string or an object.
func declaredBins(t *testing.T, manifest json5Record) []string {
t.Helper()
switch bin := manifest["bin"].(type) {
case nil:
return nil
case string:
return []string{bin}
case map[string]any:
paths := make([]string, 0, len(bin))
for _, value := range bin {
path, ok := value.(string)
if !ok {
t.Fatalf("bin entry %v is not a string", value)
}
paths = append(paths, path)
}
return paths
default:
t.Fatalf("bin has unexpected type %T", bin)
return nil
}
}

// packageBin is one bin declared by a package under packages/.
type packageBin struct {
pkg string
bin string
path string
}

// readPackageBins lists every bin declared by the packages under root/packages.
func readPackageBins(t *testing.T, root string) []packageBin {
t.Helper()
packages := filepath.Join(root, "packages")
entries, err := os.ReadDir(packages)
if err != nil {
t.Fatal(err)
}
var bins []packageBin
for _, entry := range entries {
if !entry.IsDir() {
continue
}
manifest := readJSON5Record(t, filepath.Join(packages, entry.Name(), "package.json"))
for _, bin := range declaredBins(t, manifest) {
bins = append(bins, packageBin{
pkg: entry.Name(),
bin: bin,
path: filepath.Join(packages, entry.Name(), bin),
})
}
}
return bins
}

func TestPackageBins(t *testing.T) {
root := filepath.Dir(moduleRoot(t))
bins := readPackageBins(t, root)

t.Run("reads string, object, and absent bin declarations", func(t *testing.T) {
if got := declaredBins(t, json5Record{"bin": "./cli.ts"}); len(got) != 1 || got[0] != "./cli.ts" {
t.Fatalf("string bin = %q, want [./cli.ts]", got)
}
object := declaredBins(t, json5Record{"bin": json5Record{"a": "./a.ts", "b": "./b.ts"}})
if len(object) != 2 || !containsString(object, "./a.ts") || !containsString(object, "./b.ts") {
t.Fatalf("object bin = %q, want [./a.ts ./b.ts]", object)
}
if got := declaredBins(t, json5Record{}); len(got) != 0 {
t.Fatalf("absent bin = %q, want none", got)
}
})

t.Run("at least one package declares a bin", func(t *testing.T) {
if len(bins) == 0 {
t.Fatal("no package under packages/ declares a bin")
}
})

t.Run("each bin starts with the bun shebang", func(t *testing.T) {
for _, bin := range bins {
content, err := os.ReadFile(bin.path)
if err != nil {
t.Fatal(err)
}
firstLine, _, _ := strings.Cut(string(content), "\n")
if firstLine != "#!/usr/bin/env bun" {
t.Errorf("%s bin %s first line = %q, want #!/usr/bin/env bun", bin.pkg, bin.bin, firstLine)
}
}
})

t.Run("each bin is npm-normalized without a leading ./", func(t *testing.T) {
// npm publish rewrites a "./" bin path and warns; keep manifests in the form npm publishes.
for _, bin := range bins {
if strings.HasPrefix(bin.bin, "./") {
t.Errorf("%s bin %s starts with ./", bin.pkg, bin.bin)
}
}
})
}
63 changes: 63 additions & 0 deletions go/internal/repochecks/process_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

package repochecks

import (
"bytes"
"errors"
"os"
"os/exec"
"testing"
)

// processResult holds the exit status and captured streams of a child process.
type processResult struct {
status int
stdout string
stderr string
}

// runProcess runs name in dir with env and returns its status and output.
// A nil env inherits the test process environment.
func runProcess(t *testing.T, dir string, env []string, name string, args ...string) processResult {
t.Helper()
cmd := exec.CommandContext(t.Context(), name, args...)
cmd.Dir = dir
cmd.Env = env
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
var result processResult
err := cmd.Run()
if err != nil {
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
t.Fatalf("run %s: %v", name, err)
}
result.status = exitErr.ExitCode()
if result.status < 0 {
// A process killed by a signal has no exit code, so report it as a failure.
result.status = 1
}
}
result.stdout = stdout.String()
result.stderr = stderr.String()
return result
}

func writeFixtureFile(t *testing.T, path, content string) {
t.Helper()
err := os.WriteFile(path, []byte(content), 0o600)
if err != nil {
t.Fatal(err)
}
}

func readFixtureFile(t *testing.T, path string) string {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return string(data)
}
66 changes: 66 additions & 0 deletions go/internal/repochecks/rumdl_extends_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

package repochecks

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/RigelBuild/meissa/go/internal/jstext"
)

// longParagraph is one sentence that exceeds the default MD013 line width.
const longParagraph = "A paragraph whose length exceeds the default maximum line width " +
"without violating other default rules. "

func TestSharedRumdlPolicyInheritance(t *testing.T) {
baseConfig := os.Getenv("RUMDL_BASE_CONFIG")
if baseConfig == "" {
t.Fatal("RUMDL_BASE_CONFIG is not set")
}
rumdl := os.Getenv("RUMDL_BIN")
if rumdl == "" {
t.Fatal("RUMDL_BIN is not set")
}
pathRumdl, err := exec.LookPath("rumdl")
if err != nil {
t.Fatalf("look up rumdl on PATH: %v (RUMDL_BIN %s)", err, rumdl)
}
if pathRumdl != rumdl {
t.Fatalf("PATH rumdl (%s) does not match RUMDL_BIN", pathRumdl)
}

t.Run("extends inherits disabled MD013 for a long heading-less paragraph", func(t *testing.T) {
// The TOML holds the literal placeholder; rumdl expands it from RUMDL_BASE_CONFIG.
config := "extends = \"$RUMDL_BASE_CONFIG\"\n\n[MD041]\nenabled = false\n"
result := checkWithRumdl(t, rumdl, baseConfig, config)
if result.status != 0 {
t.Fatalf("rumdl exit = %d, want 0: %s%s", result.status, result.stdout, result.stderr)
}
})

t.Run("the heading-less control without extends reports MD013", func(t *testing.T) {
result := checkWithRumdl(t, rumdl, baseConfig, "[MD041]\nenabled = false\n")
if result.status != 1 {
t.Fatalf("rumdl exit = %d, want 1: %s%s", result.status, result.stdout, result.stderr)
}
if output := result.stdout + result.stderr; !strings.Contains(output, "MD013") {
t.Fatalf("output lacks MD013: %s", output)
}
})
}

// checkWithRumdl writes config and a long heading-less paragraph, then runs rumdl check on them.
func checkWithRumdl(t *testing.T, rumdl, baseConfig, config string) processResult {
t.Helper()
directory := t.TempDir()
configPath := filepath.Join(directory, ".rumdl.toml")
markdownPath := filepath.Join(directory, "headingless.md")
writeFixtureFile(t, configPath, config)
writeFixtureFile(t, markdownPath, jstext.TrimRight(strings.Repeat(longParagraph, 2))+"\n")
env := append(os.Environ(), "RUMDL_BASE_CONFIG="+baseConfig)
return runProcess(t, directory, env, rumdl, "check", "--no-cache", "--config", configPath, markdownPath)
}
Loading