-
Notifications
You must be signed in to change notification settings - Fork 549
feat: support insert_into_file
erroring if the file is not changed, and add insert_into_file
#908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,10 @@ def invoke!(*args, &block) | |
capture(:stdout) { invoker.insert_into_file(*args, &block) } | ||
end | ||
|
||
def invoke_with_a_bang!(*args, &block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: this seemed like the most straightforward, if a little whimsical, way of handling the tests Happy to go with an alternative structure though if desired |
||
capture(:stdout) { invoker.insert_into_file!(*args, &block) } | ||
end | ||
|
||
def revoke!(*args, &block) | ||
capture(:stdout) { revoker.insert_into_file(*args, &block) } | ||
end | ||
|
@@ -170,6 +174,164 @@ def file | |
end | ||
end | ||
end | ||
|
||
context "with a bang" do | ||
it "changes the file adding content after the flag" do | ||
invoke_with_a_bang! "doc/README", "\nmore content", after: "__start__" | ||
expect(File.read(file)).to eq("__start__\nmore content\nREADME\n__end__\n") | ||
end | ||
|
||
it "changes the file adding content before the flag" do | ||
invoke_with_a_bang! "doc/README", "more content\n", before: "__end__" | ||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
end | ||
|
||
it "appends content to the file if before and after arguments not provided" do | ||
invoke_with_a_bang!("doc/README", "more content\n") | ||
expect(File.read(file)).to eq("__start__\nREADME\n__end__\nmore content\n") | ||
end | ||
|
||
it "does not change the file and raises an error if replacement present in the file" do | ||
invoke_with_a_bang!("doc/README", "more specific content\n") | ||
expect do | ||
invoke_with_a_bang!("doc/README", "more specific content\n") | ||
end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change") | ||
end | ||
|
||
it "does not change the file and raises an error if flag not found in the file" do | ||
expect do | ||
invoke_with_a_bang!("doc/README", "more content\n", after: "whatever") | ||
end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change") | ||
end | ||
|
||
it "accepts data as a block" do | ||
invoke_with_a_bang! "doc/README", before: "__end__" do | ||
"more content\n" | ||
end | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
end | ||
|
||
it "logs status" do | ||
expect(invoke_with_a_bang!("doc/README", "\nmore content", after: "__start__")).to eq(" insert doc/README\n") | ||
end | ||
|
||
it "logs status if pretending" do | ||
invoker(pretend: true) | ||
expect(invoke_with_a_bang!("doc/README", "\nmore content", after: "__start__")).to eq(" insert doc/README\n") | ||
end | ||
|
||
it "does not change the file if pretending" do | ||
invoker pretend: true | ||
invoke_with_a_bang! "doc/README", "\nmore content", after: "__start__" | ||
expect(File.read(file)).to eq("__start__\nREADME\n__end__\n") | ||
end | ||
|
||
it "does not change the file and raises an error if already includes content" do | ||
invoke_with_a_bang! "doc/README", before: "__end__" do | ||
"more content\n" | ||
end | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
|
||
expect do | ||
invoke_with_a_bang! "doc/README", before: "__end__" do | ||
"more content\n" | ||
end | ||
end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change") | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
end | ||
|
||
it "does not change the file and raises an error if already includes content using before with capture" do | ||
invoke_with_a_bang! "doc/README", before: /(__end__)/ do | ||
"more content\n" | ||
end | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
|
||
expect do | ||
invoke_with_a_bang! "doc/README", before: /(__end__)/ do | ||
"more content\n" | ||
end | ||
end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change") | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
end | ||
|
||
it "does not change the file and raises an error if already includes content using after with capture" do | ||
invoke_with_a_bang! "doc/README", after: /(README\n)/ do | ||
"more content\n" | ||
end | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
|
||
expect do | ||
invoke_with_a_bang! "doc/README", after: /(README\n)/ do | ||
"more content\n" | ||
end | ||
end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change") | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
end | ||
|
||
it "does not attempt to change the file if it doesn't exist - instead raises Thor::Error" do | ||
expect do | ||
invoke_with_a_bang! "idontexist", before: "something" do | ||
"any content" | ||
end | ||
end.to raise_error(Thor::Error, /does not appear to exist/) | ||
expect(File.exist?("idontexist")).to be_falsey | ||
end | ||
|
||
it "does not attempt to change the file if it doesn't exist and pretending" do | ||
expect do | ||
invoker pretend: true | ||
invoke_with_a_bang! "idontexist", before: "something" do | ||
"any content" | ||
end | ||
end.not_to raise_error | ||
expect(File.exist?("idontexist")).to be_falsey | ||
end | ||
|
||
it "does change the file if already includes content and :force is true" do | ||
invoke_with_a_bang! "doc/README", before: "__end__" do | ||
"more content\n" | ||
end | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n") | ||
|
||
invoke_with_a_bang! "doc/README", before: "__end__", force: true do | ||
"more content\n" | ||
end | ||
|
||
expect(File.read(file)).to eq("__start__\nREADME\nmore content\nmore content\n__end__\n") | ||
end | ||
|
||
it "can insert chinese" do | ||
encoding_original = Encoding.default_external | ||
|
||
begin | ||
silence_warnings do | ||
Encoding.default_external = Encoding.find("UTF-8") | ||
end | ||
invoke_with_a_bang! "doc/README.zh", "\n中文", after: "__start__" | ||
expect(File.read(File.join(destination_root, "doc/README.zh"))).to eq("__start__\n中文\n说明\n__end__\n") | ||
ensure | ||
silence_warnings do | ||
Encoding.default_external = encoding_original | ||
end | ||
end | ||
end | ||
|
||
it "does not mutate the provided config" do | ||
config = {after: "__start__"} | ||
invoke_with_a_bang! "doc/README", "\nmore content", config | ||
expect(File.read(file)).to eq("__start__\nmore content\nREADME\n__end__\n") | ||
|
||
expect(config).to eq({after: "__start__"}) | ||
end | ||
end | ||
end | ||
|
||
describe "#revoke!" do | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: moving this is good anyway because it was the subject of the method comment intended for
insert_into_file
😅