test/commands/edit_spec.rb in friends-0.35 vs test/commands/edit_spec.rb in friends-0.36

- old
+ new

@@ -1,27 +1,135 @@ # frozen_string_literal: true require "./test/helper" clean_describe "edit" do - subject do - stdout, stderr, status = Open3.capture3( - "EDITOR=cat bundle exec bin/friends --colorless --filename #{filename} edit" - ) - { - stdout: stdout, - stderr: stderr, - status: status.exitstatus - } + subject { run_cmd("#{quiet} edit", env_vars: "EDITOR=#{editor}") } + + let(:content) { SCRAMBLED_CONTENT } + let(:editor) { "cat" } + + # First we test some simple behavior using `cat` as our "editor." + describe "when output is quieted" do + let(:quiet) { "--quiet" } + + it 'opens the file in the "editor"' do + stdout_only content + end + + it "cleans the file" do + file_equals CONTENT # File is cleaned (no longer scrambled). + end end - let(:content) { CONTENT } + describe "when output is not quieted" do + let(:quiet) { nil } - it 'opens the file in the "editor"' do - # Because of the way that the edit command uses `Kernel.exec` to replace itself, - # our `Open3.capture3` call doesn't return STDOUT from before the `Kernel.exec` - # call, meaning we can't test output of the status message we print out, and - # our test here can check that STDOUT is equivalent to the `Kernel.exec` command's - # output, even though visually that's not what happens. - stdout_only content + # Since our "editor" is just `cat`, our STDOUT output will include both the opening + # message and the contents of the file. + it 'prints a message and opens the file in the "editor"' do + stdout_only "Opening \"#{filename}\" with \"#{editor}\""\ + "\n#{content}File cleaned: \"#{filename}\"" + end + + it "cleans the file" do + file_equals CONTENT # File is cleaned (no longer scrambled). + end + end + + describe "when editor does not exit successfully" do + let(:editor) { "'exit 1 #'" } + + describe "when output is quieted" do + let(:quiet) { "--quiet" } + + it "prints nothing to STDOUT" do + stdout_only "" + end + + it "does not clean the file" do + file_equals content + end + end + + describe "when output is not quieted" do + let(:quiet) { nil } + + it "prints a status message to STDOUT" do + stdout_only <<-OUTPUT +Opening "#{filename}" with "exit 1 #" +Not cleaning file: "#{filename}" ("exit 1 #" did not exit successfully) + OUTPUT + end + + it "does not clean the file" do + file_equals content + end + end + end + + # Now we test a more realistic scenario, in which the editor actually modifies the + # file. Instead of human interaction we use the `test/editor` file to serve as our + # editor; that script adds a new activity to the file with a friend name and location + # name that are not currently in the file. This tests the fact that `friends edit` + # should clean the `friends.md` file *after* the editor has closed successfully. + describe "when edit operation adds an event that includes a new friend or location" do + let(:editor) { "test/editor" } + let(:cleaned_edited_content) do + <<-EXPECTED_CONTENT +### Activities: +- 2015-11-01: **Grace Hopper** and I went to _Marie's Diner_. George had to cancel at the last minute. @food +- 2015-01-04: Got lunch with **Grace Hopper** and **George Washington Carver**. @food +- 2014-12-31: Celebrated the new year in _Paris_ with **Marie Curie**. @partying +- 2014-12-15: I just had a possible **Bigfoot** sighting! I think I may have seen **Bigfoot** in the _Mysterious Mountains_. +- 2014-11-15: Talked to **George Washington Carver** on the phone for an hour. + +### Notes: +- 2017-03-12: **Marie Curie** completed her PhD in record time. @school +- 2015-06-15: **Grace Hopper** found out she's getting a big Naval Academy building named after her. @navy +- 2015-06-06: **Marie Curie** just got accepted into a PhD program in _Paris_. @school +- 2015-01-04: **Grace Hopper** and **George Washington Carver** both won an award. + +### Friends: +- Bigfoot +- George Washington Carver +- Grace Hopper (a.k.a. The Admiral a.k.a. Amazing Grace) [Paris] @navy @science +- Marie Curie [Atlantis] @science + +### Locations: +- Atlantis +- Marie's Diner +- Mysterious Mountains +- Paris + EXPECTED_CONTENT + end + + describe "when output is quieted" do + let(:quiet) { "--quiet" } + + it "prints nothing to STDOUT" do + stdout_only "" + end + + it "cleans the file after the editor has quit" do + file_equals cleaned_edited_content + end + end + + describe "when output is not quieted" do + let(:quiet) { nil } + + it "prints status messages to STDOUT" do + stdout_only <<-OUTPUT +Opening "#{filename}" with "#{editor}" +Friend added: "Bigfoot" +Location added: "Mysterious Mountains" +File cleaned: "#{filename}" + OUTPUT + end + + it "cleans the file after the editor has quit" do + file_equals cleaned_edited_content + end + end end end