feat(pyproject): release v1.7.0 — Python pyproject.toml version bump
Add internal/pyproject package with ReadVersion and WriteVersion for pyproject.toml files. Reads [project].version (PEP 621) first, then falls back to [tool.poetry].version. Section boundaries are detected via TOML's rule that headers always start at the beginning of a line (\n[ pattern), so inline arrays with [ characters don't interfere. Config follows the established multi-value pattern: - python.pyproject_toml: single path (opt-in, no default) - python.pyproject_tomls: list for monorepos (overrides single) - --pyproject flag overrides pyproject_toml and clears pyproject_tomls 100% per-package statement coverage maintained across all 14 packages; FuzzReadVersion and FuzzWriteVersion added per fuzzing guidelines. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,8 +17,9 @@ type Config struct {
|
||||
Git GitConfig `yaml:"git"`
|
||||
Maven MavenConfig `yaml:"maven"`
|
||||
Node NodeConfig `yaml:"node"`
|
||||
Gradle GradleConfig `yaml:"gradle"`
|
||||
GitLab GitLabConfig `yaml:"gitlab"`
|
||||
Gradle GradleConfig `yaml:"gradle"`
|
||||
Python PythonConfig `yaml:"python"`
|
||||
GitLab GitLabConfig `yaml:"gitlab"`
|
||||
GitHub GitHubConfig `yaml:"github"`
|
||||
}
|
||||
|
||||
@@ -91,6 +92,23 @@ func (g GradleConfig) EffectiveBuildFiles() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
type PythonConfig struct {
|
||||
PyprojectTOML string `yaml:"pyproject_toml"` // single path (opt-in, no default)
|
||||
PyprojectTOMLs []string `yaml:"pyproject_tomls"` // multiple paths; overrides PyprojectTOML
|
||||
}
|
||||
|
||||
// EffectivePaths returns the list of pyproject.toml paths to process.
|
||||
// Returns nil when no python paths are configured (python processing is opt-in).
|
||||
func (p PythonConfig) EffectivePaths() []string {
|
||||
if len(p.PyprojectTOMLs) > 0 {
|
||||
return p.PyprojectTOMLs
|
||||
}
|
||||
if p.PyprojectTOML != "" {
|
||||
return []string{p.PyprojectTOML}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type GitLabConfig struct {
|
||||
URL string `yaml:"url"`
|
||||
Token string `yaml:"token"`
|
||||
@@ -135,9 +153,11 @@ func defaultSources() Sources {
|
||||
"maven.pom_paths": "default",
|
||||
"node.package_json": "default",
|
||||
"node.package_jsons": "default",
|
||||
"gradle.build_file": "default",
|
||||
"gradle.build_files": "default",
|
||||
"gitlab.url": "default",
|
||||
"gradle.build_file": "default",
|
||||
"gradle.build_files": "default",
|
||||
"python.pyproject_toml": "default",
|
||||
"python.pyproject_tomls": "default",
|
||||
"gitlab.url": "default",
|
||||
"gitlab.token": "default",
|
||||
"gitlab.project": "default",
|
||||
"github.token": "default",
|
||||
@@ -219,6 +239,12 @@ func LoadWithSources(dir string) (Config, Sources, error) {
|
||||
if len(overlay.Gradle.BuildFiles) > 0 {
|
||||
src["gradle.build_files"] = "config file"
|
||||
}
|
||||
if overlay.Python.PyprojectTOML != "" {
|
||||
src["python.pyproject_toml"] = "config file"
|
||||
}
|
||||
if len(overlay.Python.PyprojectTOMLs) > 0 {
|
||||
src["python.pyproject_tomls"] = "config file"
|
||||
}
|
||||
if overlay.GitLab.URL != "" {
|
||||
src["gitlab.url"] = "config file"
|
||||
}
|
||||
|
||||
@@ -338,6 +338,49 @@ func TestLoadGradleBuildFilesSources(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPythonEffectivePaths(t *testing.T) {
|
||||
if got := (PythonConfig{}).EffectivePaths(); got != nil {
|
||||
t.Errorf("empty config: got %v, want nil", got)
|
||||
}
|
||||
if got := (PythonConfig{PyprojectTOML: "pyproject.toml"}).EffectivePaths(); len(got) != 1 || got[0] != "pyproject.toml" {
|
||||
t.Errorf("single path: got %v", got)
|
||||
}
|
||||
p := PythonConfig{PyprojectTOML: "pyproject.toml", PyprojectTOMLs: []string{"a/pyproject.toml", "b/pyproject.toml"}}
|
||||
if got := p.EffectivePaths(); len(got) != 2 || got[0] != "a/pyproject.toml" {
|
||||
t.Errorf("multi-path priority: got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadPythonSources(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
content := "python:\n pyproject_toml: \"pyproject.toml\"\n"
|
||||
if err := os.WriteFile(filepath.Join(dir, filename), []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, src, err := LoadWithSources(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if src["python.pyproject_toml"] != "config file" {
|
||||
t.Errorf("src[python.pyproject_toml] = %q, want %q", src["python.pyproject_toml"], "config file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadPythonPathsSources(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
content := "python:\n pyproject_tomls:\n - \"a/pyproject.toml\"\n - \"b/pyproject.toml\"\n"
|
||||
if err := os.WriteFile(filepath.Join(dir, filename), []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, src, err := LoadWithSources(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if src["python.pyproject_tomls"] != "config file" {
|
||||
t.Errorf("src[python.pyproject_tomls] = %q, want %q", src["python.pyproject_tomls"], "config file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadPartialOverride(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// Only override tag_prefix — commit_message should keep its default
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package pyproject
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// sectionRe matches a TOML section from its header to the next header (or end of file).
|
||||
// TOML section headers always appear at the start of a line, so \n[ is a reliable boundary.
|
||||
var (
|
||||
projectSectionRe = regexp.MustCompile(`(?s)\[project\].*?(?:\n\[|\z)`)
|
||||
poetrySectionRe = regexp.MustCompile(`(?s)\[tool\.poetry\].*?(?:\n\[|\z)`)
|
||||
)
|
||||
|
||||
// versionLineRe matches version = "x.y.z" on its own line inside a section.
|
||||
var versionLineRe = regexp.MustCompile(`(?m)^version\s*=\s*"([^"]+)"`)
|
||||
|
||||
// ReadVersion returns the project version from a pyproject.toml file.
|
||||
// It checks [project] (PEP 621) first, then [tool.poetry] (Poetry).
|
||||
func ReadVersion(path string) (string, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
content := string(data)
|
||||
for _, sectionRe := range []*regexp.Regexp{projectSectionRe, poetrySectionRe} {
|
||||
section := sectionRe.FindString(content)
|
||||
if section == "" {
|
||||
continue
|
||||
}
|
||||
m := versionLineRe.FindStringSubmatch(section)
|
||||
if m != nil {
|
||||
return m[1], nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("no version found in %s", path)
|
||||
}
|
||||
|
||||
// WriteVersion replaces the project version in a pyproject.toml file in-place.
|
||||
// oldVersion must match what ReadVersion returned.
|
||||
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 version in the first [project] or [tool.poetry]
|
||||
// section that contains it. Returns the updated content and true on success.
|
||||
// regexp.QuoteMeta is used so version strings with dots or specials are safe.
|
||||
func replaceVersion(content, oldVersion, newVersion string) (string, bool) {
|
||||
re := regexp.MustCompile(`(?m)^(version\s*=\s*)"` + regexp.QuoteMeta(oldVersion) + `"`)
|
||||
for _, sectionRe := range []*regexp.Regexp{projectSectionRe, poetrySectionRe} {
|
||||
loc := sectionRe.FindStringIndex(content)
|
||||
if loc == nil {
|
||||
continue
|
||||
}
|
||||
section := content[loc[0]:loc[1]]
|
||||
m := re.FindStringSubmatchIndex(section)
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
// m[0]:m[1] = full match; m[2]:m[3] = group 1 (prefix "version = ")
|
||||
newSection := section[:m[0]] + section[m[2]:m[3]] + `"` + newVersion + `"` + section[m[1]:]
|
||||
return content[:loc[0]] + newSection + content[loc[1]:], true
|
||||
}
|
||||
return content, false
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package pyproject
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const pyprojectPEP621 = `[project]
|
||||
name = "my-package"
|
||||
version = "1.2.3"
|
||||
description = "A test package"
|
||||
dependencies = ["requests>=2.0", "click>=8.0"]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
`
|
||||
|
||||
const pyprojectPoetry = `[tool.poetry]
|
||||
name = "my-package"
|
||||
version = "1.2.3"
|
||||
description = "A test package"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.9"
|
||||
requests = "^2.0"
|
||||
`
|
||||
|
||||
const pyprojectBoth = `[project]
|
||||
name = "my-package"
|
||||
version = "1.2.3"
|
||||
|
||||
[tool.poetry]
|
||||
name = "my-package"
|
||||
version = "1.2.3"
|
||||
`
|
||||
|
||||
const pyprojectBuildSystemFirst = `[build-system]
|
||||
requires = ["hatchling"]
|
||||
|
||||
[project]
|
||||
name = "my-package"
|
||||
version = "1.2.3"
|
||||
`
|
||||
|
||||
func writePyproject(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "pyproject.toml")
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// ── ReadVersion ───────────────────────────────────────────────────────────────
|
||||
|
||||
func TestReadVersionPEP621(t *testing.T) {
|
||||
got, err := ReadVersion(writePyproject(t, pyprojectPEP621))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.2.3" {
|
||||
t.Errorf("got %q, want 1.2.3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionPoetry(t *testing.T) {
|
||||
got, err := ReadVersion(writePyproject(t, pyprojectPoetry))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.2.3" {
|
||||
t.Errorf("got %q, want 1.2.3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionPEP621TakesPrecedence(t *testing.T) {
|
||||
// When both [project] and [tool.poetry] are present, [project] wins.
|
||||
got, err := ReadVersion(writePyproject(t, pyprojectBoth))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.2.3" {
|
||||
t.Errorf("got %q, want 1.2.3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionBuildSystemIgnored(t *testing.T) {
|
||||
// version under [build-system] must not be returned.
|
||||
got, err := ReadVersion(writePyproject(t, pyprojectBuildSystemFirst))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "1.2.3" {
|
||||
t.Errorf("got %q, want 1.2.3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionMissingFile(t *testing.T) {
|
||||
_, err := ReadVersion(filepath.Join(t.TempDir(), "pyproject.toml"))
|
||||
if err == nil {
|
||||
t.Error("expected error for missing file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadVersionNoVersion(t *testing.T) {
|
||||
_, err := ReadVersion(writePyproject(t, `[build-system]
|
||||
requires = ["hatchling"]
|
||||
`))
|
||||
if err == nil {
|
||||
t.Error("expected error when no version is found")
|
||||
}
|
||||
}
|
||||
|
||||
// ── WriteVersion ──────────────────────────────────────────────────────────────
|
||||
|
||||
func TestWriteVersionPEP621(t *testing.T) {
|
||||
path := writePyproject(t, pyprojectPEP621)
|
||||
if err := WriteVersion(path, "1.2.3", "1.2.4"); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
data, _ := os.ReadFile(path)
|
||||
s := string(data)
|
||||
if !strings.Contains(s, `version = "1.2.4"`) {
|
||||
t.Errorf("expected updated version; got:\n%s", s)
|
||||
}
|
||||
// build-system section must not be touched
|
||||
if strings.Contains(s, `"hatchling>=1.2.4"`) {
|
||||
t.Error("build-system section was incorrectly modified")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionPoetry(t *testing.T) {
|
||||
path := writePyproject(t, pyprojectPoetry)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionBothSectionsUpdatesProject(t *testing.T) {
|
||||
path := writePyproject(t, pyprojectBoth)
|
||||
if err := WriteVersion(path, "1.2.3", "1.2.4"); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
data, _ := os.ReadFile(path)
|
||||
s := string(data)
|
||||
// [project] version updated
|
||||
if !strings.Contains(s, `[project]`) || strings.Index(s, `version = "1.2.4"`) > strings.Index(s, `[tool.poetry]`) {
|
||||
t.Error("expected [project] version to be updated first")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionMissingFile(t *testing.T) {
|
||||
err := WriteVersion(filepath.Join(t.TempDir(), "pyproject.toml"), "1.0.0", "1.0.1")
|
||||
if err == nil {
|
||||
t.Error("expected error for missing file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionNotFound(t *testing.T) {
|
||||
path := writePyproject(t, pyprojectPEP621)
|
||||
err := WriteVersion(path, "9.9.9", "9.9.10")
|
||||
if err == nil {
|
||||
t.Error("expected error when old version not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteVersionReadOnly(t *testing.T) {
|
||||
if os.Getuid() == 0 {
|
||||
t.Skip("skipping: chmod restrictions do not apply when running as root")
|
||||
}
|
||||
path := writePyproject(t, pyprojectPEP621)
|
||||
os.Chmod(path, 0444)
|
||||
defer os.Chmod(path, 0644)
|
||||
if err := WriteVersion(path, "1.2.3", "1.2.4"); err == nil {
|
||||
t.Error("expected error writing to read-only file")
|
||||
}
|
||||
}
|
||||
|
||||
// ── replaceVersion ────────────────────────────────────────────────────────────
|
||||
|
||||
func TestReplaceVersionNotFound(t *testing.T) {
|
||||
content := `[build-system]
|
||||
requires = ["hatchling"]
|
||||
`
|
||||
got, ok := replaceVersion(content, "1.0.0", "1.0.1")
|
||||
if ok {
|
||||
t.Error("expected ok=false when no matching section")
|
||||
}
|
||||
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(pyprojectPEP621)
|
||||
f.Add(pyprojectPoetry)
|
||||
f.Add(pyprojectBoth)
|
||||
f.Add(`[project]` + "\n")
|
||||
f.Add("")
|
||||
f.Add("\x00\xff")
|
||||
|
||||
f.Fuzz(func(t *testing.T, content string) {
|
||||
path := filepath.Join(t.TempDir(), "pyproject.toml")
|
||||
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(pyprojectPEP621, "1.2.3", "1.2.4")
|
||||
f.Add(pyprojectPoetry, "1.2.3", "1.2.4")
|
||||
f.Add(pyprojectBoth, "1.2.3", "1.2.4")
|
||||
f.Add("", "1.0.0", "1.0.1")
|
||||
f.Add(`[project]`+"\n"+"version = \"1.0.0\"\n", "1.0.0", "1.0.1")
|
||||
|
||||
f.Fuzz(func(t *testing.T, content, oldVersion, newVersion string) {
|
||||
path := filepath.Join(t.TempDir(), "pyproject.toml")
|
||||
os.WriteFile(path, []byte(content), 0644) //nolint:errcheck
|
||||
WriteVersion(path, oldVersion, newVersion) //nolint:errcheck
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user