Skip to content
Open
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
9 changes: 3 additions & 6 deletions internal/librarian/rust/update_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ func updateWorkspaceVersion(path, crateName string, newVersion semver.Version) e
}
lines := strings.Split(string(contents), "\n")
updated := false
crateRegex := regexp.MustCompile(fmt.Sprintf(`^\s*%s\s*=`, regexp.QuoteMeta(crateName)))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use string matching with strings.HasPrefix instead of regex, since it is more transparent and easier to understand that regex

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what you were looking for? Or did you also want to convert the isPackage check to prefix matching?

	lines := strings.Split(string(contents), "\n")
	updated := false
	packageRegex := regexp.MustCompile(fmt.Sprintf(`^\s*[^#]*\bpackage\s*=\s*"%s"`, regexp.QuoteMeta(crateName)))
	for i, line := range lines {
		trimmed := strings.TrimSpace(line)

		// Check if line starts with: crateName = ...
		isCrate := false
		if strings.HasPrefix(trimmed, crateName) {
			after := strings.TrimSpace(trimmed[len(crateName):])
			if strings.HasPrefix(after, "=") {
				isCrate = true
			}
		}

		// Check if line matches package regex
		isPackage := packageRegex.MatchString(line)
if (isCrate || isPackage) && versionRegex.MatchString(line) {

packageRegex := regexp.MustCompile(fmt.Sprintf(`^\s*[^#]*\bpackage\s*=\s*"%s"`, regexp.QuoteMeta(crateName)))
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if !strings.HasPrefix(trimmed, crateName) {
continue
}
after := strings.TrimSpace(trimmed[len(crateName):])
if strings.HasPrefix(after, "=") && versionRegex.MatchString(line) {
if (crateRegex.MatchString(line) || packageRegex.MatchString(line)) && versionRegex.MatchString(line) {
lines[i] = versionRegex.ReplaceAllString(line, fmt.Sprintf(`version = "%s"`, newVersion.String()))
updated = true
}
Expand Down
18 changes: 18 additions & 0 deletions internal/librarian/rust/update_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ func TestUpdateWorkspaceVersion(t *testing.T) {
crateName: "test-crate",
want: "[workspace.dependencies]\nother-crate = { version = \"1.0.0\", path = \"src/other-crate\" }\n",
},
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: with regex I add a test for "not-want"

name: "renamed dependency",
content: "[workspace.dependencies]\nwkt = { version = \"1.0.0\", package = \"google-cloud-wkt\" }\n",
crateName: "google-cloud-wkt",
want: "[workspace.dependencies]\nwkt = { version = \"2.0.0\", package = \"google-cloud-wkt\" }\n",
},
{
name: "renamed dependency reverse order",
content: "[workspace.dependencies]\nwkt = { package = \"google-cloud-wkt\", version = \"1.0.0\" }\n",
crateName: "google-cloud-wkt",
want: "[workspace.dependencies]\nwkt = { package = \"google-cloud-wkt\", version = \"2.0.0\" }\n",
},
{
name: "inline comment ignored",
content: "[workspace.dependencies]\nother-crate = { version = \"1.0.0\" } # package = \"google-cloud-wkt\"\n",
crateName: "google-cloud-wkt",
want: "[workspace.dependencies]\nother-crate = { version = \"1.0.0\" } # package = \"google-cloud-wkt\"\n",
},
} {
t.Run(test.name, func(t *testing.T) {
filePath := setupTestCargoFile(t, test.content)
Expand Down
Loading