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:
@@ -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