Skip to content

Commit

Permalink
Fix: Fix updating JS versions with single quotes
Browse files Browse the repository at this point in the history
When updating a version in a version.js file support single and double
quotes.
  • Loading branch information
bjoernricks committed Jun 29, 2023
1 parent 952a0a7 commit c0eaabc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pontos/version/commands/_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,14 @@ def _update_version_file(
return False

content = version_file.read_text(encoding="utf-8")
content = re.sub(
pattern=r'VERSION = "(?P<version>.*)"',
repl=f'VERSION = "{new_version}"',
match = re.search(
pattern=r'VERSION = (?P<quote>[\'"])(?P<version>.*)(?P=quote)',
string=content,
)
if not match:
return False

content = content.replace(match.group("version"), str(new_version))
version_file.write_text(content, encoding="utf-8")
return True

Expand Down
47 changes: 47 additions & 0 deletions tests/version/commands/test_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,53 @@ def test_update_js_version_file(self):
"const func = () => ();\n",
)

def test_update_js_version_file_with_single_quotes(self):
content = '{"name":"foo", "version":"1.2.3"}'
js_content = """const foo = "bar";
const VERSION = '1.2.3';
const func = () => ();
"""

with temp_directory(change_into=True) as temp_dir:
package_json = temp_dir / "package.json"
package_json.write_text(content, encoding="utf8")
js_version_file = (
temp_dir / JavaScriptVersionCommand.version_file_paths[0]
)
js_version_file.parent.mkdir()
js_version_file.write_text(js_content, encoding="utf8")

cmd = JavaScriptVersionCommand(SemanticVersioningScheme)
updated = cmd.update_version(
SemanticVersioningScheme.parse_version("22.4.0")
)

self.assertEqual(
updated.previous,
SemanticVersioningScheme.parse_version("1.2.3"),
)
self.assertEqual(
updated.new, SemanticVersioningScheme.parse_version("22.4.0")
)
self.assertEqual(
updated.changed_files,
[
package_json.resolve(),
JavaScriptVersionCommand.version_file_paths[0],
],
)

with package_json.open(mode="r", encoding="utf-8") as fp:
fake_package = json.load(fp)

self.assertEqual(fake_package["version"], "22.4.0")

self.assertEqual(
js_version_file.read_text(encoding="utf8"),
"const foo = \"bar\";\nconst VERSION = '22.4.0';\n"
"const func = () => ();\n",
)

def test_update_version_files(self):
content = '{"name":"foo", "version":"1.2.3"}'
file_content = """const foo = "bar";
Expand Down

0 comments on commit c0eaabc

Please sign in to comment.