feat(gradle): release v1.6.0 — Gradle build file version bump
Add internal/gradle package with ReadVersion and WriteVersion for build.gradle (Groovy DSL, single-quoted) and build.gradle.kts (Kotlin DSL, double-quoted). Quote style is preserved on write. regexp.QuoteMeta ensures version strings with dots or special characters are safe. Config follows the established multi-value pattern: - gradle.build_file: single path (opt-in, no default) - gradle.build_files: list for multi-module projects (overrides build_file) - --gradle flag overrides build_file and clears build_files 100% per-package statement coverage maintained across all 13 packages; FuzzReadVersion and FuzzWriteVersion added per fuzzing guidelines. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package gradle
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// versionRe matches a Gradle/Kotlin DSL version assignment on its own line.
|
||||
// Group 1 captures the version string (without quotes).
|
||||
// Handles both double-quoted (Kotlin/Groovy) and single-quoted (Groovy) forms,
|
||||
// with or without spaces around =.
|
||||
var versionRe = regexp.MustCompile(`(?m)^[ \t]*version\s*=\s*["']([^"']+)["']`)
|
||||
|
||||
// ReadVersion returns the version value from a build.gradle or build.gradle.kts file.
|
||||
func ReadVersion(path string) (string, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
m := versionRe.FindStringSubmatch(string(data))
|
||||
if m == nil {
|
||||
return "", fmt.Errorf("no version assignment found in %s", path)
|
||||
}
|
||||
return m[1], nil
|
||||
}
|
||||
|
||||
// WriteVersion replaces the version assignment in a build.gradle or build.gradle.kts
|
||||
// file in-place. oldVersion must match what ReadVersion returned. Quote style is preserved.
|
||||
func WriteVersion(path, oldVersion, newVersion string) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
updated, ok := replaceVersion(string(data), oldVersion, newVersion)
|
||||
if !ok {
|
||||
return fmt.Errorf("version %q not found in %s", oldVersion, path)
|
||||
}
|
||||
return os.WriteFile(path, []byte(updated), 0644)
|
||||
}
|
||||
|
||||
// replaceVersion finds and replaces the first version assignment line in a Gradle build file.
|
||||
// Quote style (single or double) of the original line is preserved.
|
||||
// Returns the updated content and true if a replacement was made.
|
||||
func replaceVersion(content, oldVersion, newVersion string) (string, bool) {
|
||||
re := regexp.MustCompile(`(?m)^([ \t]*version\s*=\s*)(["'])` + regexp.QuoteMeta(oldVersion) + `["']`)
|
||||
m := re.FindStringSubmatchIndex(content)
|
||||
if m == nil {
|
||||
return content, false
|
||||
}
|
||||
prefix := content[m[2]:m[3]] // "version = " etc., preserving whitespace
|
||||
quote := content[m[4]:m[5]] // " or '
|
||||
return content[:m[0]] + prefix + quote + newVersion + quote + content[m[1]:], true
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package gradle
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const gradleGroovy = `plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group = 'com.example'
|
||||
version = '1.2.3'
|
||||
description = 'My project'
|
||||
`
|
||||
|
||||
const gradleKotlin = `plugins {
|
||||
kotlin("jvm") version "1.9.0"
|
||||
}
|
||||
|
||||
group = "com.example"
|
||||
version = "1.2.3"
|
||||
description = "My project"
|
||||
`
|
||||
|
||||
func writeGradle(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "build.gradle")
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// ── ReadVersion ──────────────────────────────────────────────────────────────
|
||||
|
||||
func TestReadVersionGroovy(t *testing.T) {
|
||||
got, err := ReadVersion(writeGradle(t, gradleGroovy))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.2.3" {
|
||||
t.Errorf("got %q, want 1.2.3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionKotlin(t *testing.T) {
|
||||
got, err := ReadVersion(writeGradle(t, gradleKotlin))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.2.3" {
|
||||
t.Errorf("got %q, want 1.2.3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionNoSpaces(t *testing.T) {
|
||||
got, err := ReadVersion(writeGradle(t, `version="1.0.0"`))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.0.0" {
|
||||
t.Errorf("got %q, want 1.0.0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionMissingFile(t *testing.T) {
|
||||
_, err := ReadVersion(filepath.Join(t.TempDir(), "build.gradle"))
|
||||
if err == nil {
|
||||
t.Error("expected error for missing file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionNoVersionField(t *testing.T) {
|
||||
_, err := ReadVersion(writeGradle(t, `group = "com.example"`))
|
||||
if err == nil {
|
||||
t.Error("expected error when no version assignment is present")
|
||||
}
|
||||
}
|
||||
|
||||
// ── WriteVersion ─────────────────────────────────────────────────────────────
|
||||
|
||||
func TestWriteVersionKotlin(t *testing.T) {
|
||||
path := writeGradle(t, gradleKotlin)
|
||||
if err := WriteVersion(path, "1.2.3", "1.2.4"); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
data, _ := os.ReadFile(path)
|
||||
if !strings.Contains(string(data), `version = "1.2.4"`) {
|
||||
t.Errorf("expected updated version; got:\n%s", data)
|
||||
}
|
||||
// plugin version declaration must not be touched
|
||||
if !strings.Contains(string(data), `kotlin("jvm") version "1.9.0"`) {
|
||||
t.Error("plugin version was incorrectly modified")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionGroovy(t *testing.T) {
|
||||
path := writeGradle(t, gradleGroovy)
|
||||
if err := WriteVersion(path, "1.2.3", "1.2.4"); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
data, _ := os.ReadFile(path)
|
||||
if !strings.Contains(string(data), `version = '1.2.4'`) {
|
||||
t.Errorf("expected updated version with single quotes; got:\n%s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionMissingFile(t *testing.T) {
|
||||
err := WriteVersion(filepath.Join(t.TempDir(), "build.gradle"), "1.0.0", "1.0.1")
|
||||
if err == nil {
|
||||
t.Error("expected error for missing file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionNotFound(t *testing.T) {
|
||||
path := writeGradle(t, gradleKotlin)
|
||||
err := WriteVersion(path, "9.9.9", "9.9.10")
|
||||
if err == nil {
|
||||
t.Error("expected error when old version not found in file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionReadOnly(t *testing.T) {
|
||||
path := writeGradle(t, gradleKotlin)
|
||||
os.Chmod(path, 0444)
|
||||
defer os.Chmod(path, 0644)
|
||||
err := WriteVersion(path, "1.2.3", "1.2.4")
|
||||
if err == nil {
|
||||
t.Error("expected error writing to read-only file")
|
||||
}
|
||||
}
|
||||
|
||||
// ── replaceVersion ────────────────────────────────────────────────────────────
|
||||
|
||||
func TestReplaceVersionNotFound(t *testing.T) {
|
||||
content := `group = "com.example"`
|
||||
got, ok := replaceVersion(content, "1.0.0", "1.0.1")
|
||||
if ok {
|
||||
t.Error("expected ok=false when version not present")
|
||||
}
|
||||
if got != content {
|
||||
t.Error("content should be unchanged when not found")
|
||||
}
|
||||
}
|
||||
|
||||
// ── fuzz ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
// FuzzReadVersion verifies ReadVersion never panics on arbitrary file content.
|
||||
func FuzzReadVersion(f *testing.F) {
|
||||
f.Add(gradleGroovy)
|
||||
f.Add(gradleKotlin)
|
||||
f.Add(`version="1.0.0"`)
|
||||
f.Add(`group = "com.example"`)
|
||||
f.Add("")
|
||||
f.Add("\x00\xff")
|
||||
|
||||
f.Fuzz(func(t *testing.T, content string) {
|
||||
path := filepath.Join(t.TempDir(), "build.gradle")
|
||||
os.WriteFile(path, []byte(content), 0644) //nolint:errcheck
|
||||
ReadVersion(path) //nolint:errcheck
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzWriteVersion verifies WriteVersion never panics on arbitrary content or version strings.
|
||||
func FuzzWriteVersion(f *testing.F) {
|
||||
f.Add(gradleGroovy, "1.2.3", "1.2.4")
|
||||
f.Add(gradleKotlin, "1.2.3", "1.2.4")
|
||||
f.Add(`version="1.0.0"`, "1.0.0", "1.0.1")
|
||||
f.Add("", "1.0.0", "1.0.1")
|
||||
f.Add(`version = "1.0.0"`, "", "1.0.1")
|
||||
|
||||
f.Fuzz(func(t *testing.T, content, oldVersion, newVersion string) {
|
||||
path := filepath.Join(t.TempDir(), "build.gradle")
|
||||
os.WriteFile(path, []byte(content), 0644) //nolint:errcheck
|
||||
WriteVersion(path, oldVersion, newVersion) //nolint:errcheck
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user