Skip to content

Commit

Permalink
Fix changeset filename generation (#43)
Browse files Browse the repository at this point in the history
When fetching some metadata from Git for a certain changeset, surround
the filename in quotes and escape any quotes in the filename so that
quotes don't break the Git command.

Also escape a couple more characters during file generation, but this is
extra since people can manually create filenames with whatever
characters they want.

I haven't stripped out all non-`\w` characters, because that would strip
out too many valid characters.

Fixes #36
  • Loading branch information
tombruijn authored Dec 2, 2021
1 parent d28bdf7 commit 393d60d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changesets/fix-changeset-filenames-with-symbols.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "fix"
---

Fix publishing with changeset filenames containing unescaped symbols.
4 changes: 3 additions & 1 deletion lib/mono/changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def date
def commit
@commit ||=
begin
git_log = `git log -n 1 --pretty="format:%h %H %cI" -- #{path}`
escaped_path = path.gsub('"', '\"')
git_log =
`git log -n 1 --pretty="format:%h %H %cI" -- "#{escaped_path}"`
short, long, date = git_log.split(" ")
{
:short => short,
Expand Down
2 changes: 1 addition & 1 deletion lib/mono/cli/changeset/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def execute
FileUtils.touch(File.join(dir, ".gitkeep"))
change_description =
required_input("Summarize the change (for changeset filename): ")
filename = change_description.downcase.tr(". /\\", "-")
filename = change_description.downcase.tr(".,'\" /\\", "-")
filepath = File.join(dir, "#{filename}.md")
type = prompt_for_type
bump = prompt_for_bump
Expand Down
20 changes: 19 additions & 1 deletion spec/lib/mono/changeset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def supported_bump?(bump)
end

describe "#commit" do
it "returns a hash with commit metadat" do
it "returns a hash with commit metadata" do
prepare_project :nodejs_npm_single
in_project do
path = add_changeset :patch
Expand All @@ -294,5 +294,23 @@ def supported_bump?(bump)
expect(commit[:short].length).to be < 40
end
end

it "returns a hash with commit metadata" do
prepare_project :nodejs_npm_single
in_project do
path = add_changeset :patch, :filename => "Patch, & \"messagé'"

changeset = described_class.parse(path)
commit = changeset.commit
expect(commit).to match(
:date => kind_of(Time),
:long => kind_of(String),
:short => kind_of(String)
)
expect(commit[:long].length).to be >= 40
expect(commit[:short].length).to be >= 7
expect(commit[:short].length).to be < 40
end
end
end
end
24 changes: 24 additions & 0 deletions spec/lib/mono/cli/changeset/add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@
expect(exit_status).to eql(0), output
end
end

context "with special symbols in the description" do
it "creates a file with a sanitized filename" do
prepare_project :elixir_single

add_cli_input "My \"Awes/o\\mé', patch"
add_cli_input "1" # Type: "add"
add_cli_input "patch"
add_cli_input "y"
output =
capture_stdout do
in_project { run_changeset_add }
end

changeset_path = ".changesets/my--awes-o-mé---patch.md"
expect(output).to include(
"Opening ./#{changeset_path} with editor..."
)
expect(performed_commands).to eql([
["/elixir_single_project", "$EDITOR ./#{changeset_path}"]
])
expect(exit_status).to eql(0), output
end
end
end

context "with mono repo" do
Expand Down
5 changes: 3 additions & 2 deletions spec/support/helpers/changeset_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

module ChangesetHelper
def add_changeset(bump, type: :add, message: nil)
def add_changeset(bump, type: :add, message: nil, filename: nil)
@changeset_count ||= 0
@changeset_count += 1
FileUtils.mkdir_p(".changesets")
path = ".changesets/#{@changeset_count}_#{bump}.md"
filename ||= "#{@changeset_count}_#{bump}"
path = ".changesets/#{filename}.md"
unless bump == :none
metadata = <<~METADATA
---
Expand Down

0 comments on commit 393d60d

Please sign in to comment.