Skip to content
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

Add output to file feature #47

Merged
merged 23 commits into from
Sep 29, 2020
Merged

Add output to file feature #47

merged 23 commits into from
Sep 29, 2020

Conversation

manuca
Copy link
Contributor

@manuca manuca commented Sep 17, 2020

Adds output to file option, usage:

skunk --out=file.txt

Fixes #45

@manuca manuca requested a review from bronzdoc as a code owner September 17, 2020 18:59
@codecov
Copy link

codecov bot commented Sep 17, 2020

Codecov Report

Merging #47 into main will increase coverage by 9.34%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #47      +/-   ##
==========================================
+ Coverage   82.29%   91.64%   +9.34%     
==========================================
  Files          19       20       +1     
  Lines         305      323      +18     
==========================================
+ Hits          251      296      +45     
+ Misses         54       27      -27     
Impacted Files Coverage Δ
test/lib/skunk/cli/commands/help_test.rb 100.00% <ø> (ø)
lib/skunk/cli/application.rb 100.00% <100.00%> (ø)
lib/skunk/cli/options/argv.rb 81.81% <100.00%> (+5.34%) ⬆️
test/lib/skunk/cli/options/argv_test.rb 100.00% <100.00%> (ø)
lib/skunk/cli/commands/default.rb 100.00% <0.00%> (+100.00%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e038ce3...ead3f3d. Read the comment docs.

Copy link
Contributor

@bronzdoc bronzdoc left a comment

Choose a reason for hiding this comment

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

Looks good @manuca just some minot comments

lib/skunk/cli/options/argv.rb Outdated Show resolved Hide resolved
skunk.gemspec Outdated Show resolved Hide resolved
@manuca manuca requested a review from bronzdoc September 18, 2020 18:16
Copy link
Member

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

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

@manuca Looks good. Just added a couple of comments.

lib/skunk/cli/options/argv.rb Outdated Show resolved Hide resolved
lib/skunk/cli/options/argv.rb Outdated Show resolved Hide resolved
parser = Skunk::Cli::Options::Argv.new(argv)
parser.parse
_(parser.output_filename).must_equal "file.txt"
end
Copy link
Member

Choose a reason for hiding this comment

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

@manuca Could you add another check to make sure that the content of file.txt is the right content?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@etagwerker When I created this test I thought about this but realized the report output seems difficult to make deterministic because of the dependency on git commits for instance. I saw we created and injected a mock status reporter to make the output deterministic in status_reporter_test.rb but I don't see this possible at this level of testing.

Do you have ideas on how it would be best to test the output at this level?

Copy link
Member

Choose a reason for hiding this comment

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

@manuca you can probably get around the non-deterministic problem with git commits by stubbing the method that calculates the churn of the file.

@etagwerker etagwerker changed the base branch from master to main September 24, 2020 02:57
FileUtils.rm("tmp/generated_report.txt", force: true)
FileUtils.mkdir_p("tmp")

RubyCritic::AnalysedModule.stub_any_instance(:churn, 1) do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@etagwerker I added these stubs to address your comments about testing the generated content in the files. Note I applied these at the level of the application that is where the file is output, not in the argv parser as you suggested.

end
end

_(File.read("tmp/generated_report.txt")).must_equal File.read("test/samples/console_output.txt")
Copy link
Contributor Author

@manuca manuca Sep 28, 2020

Choose a reason for hiding this comment

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

@etagwerker @bronzdoc I've been banging my head with this assertion, I can't make it pass.

I'm not being able to read any contents from tmp/generated_report.txt, they are consistently empty ("").

After the tests finish I can confirm the contents are flushed to the file, but not within the test. This is weird. I tried doing #flush on the output stream without success. I also tried using the fsync method of IO, but nothing.

Do you have any ideas? 🦆

This was the flush call I added, but now removed:
9e11eec#diff-fc8fbe9f52913e11298c9a0d6a140818L23

Copy link
Contributor

Choose a reason for hiding this comment

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

looking into it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bronzdoc what happened with those commits, I noticed the history got rewritten on this branch. Did you do a rebase against master? Next time please let's get on the same page before doing commits that break history on branches we might be sharing to avoid conflicts.

Regarding the assertion that wasn't passing on my test, did you figure an explanation for why it wasn't working? I noticed test are green now.

Copy link
Contributor

@bronzdoc bronzdoc Sep 29, 2020

Choose a reason for hiding this comment

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

@manuca what commits? the commits are there. I just removed one commit that I made that was not needed. Beside that I fixed the merge conflicts and a force push had to be made.

I think the issue was that we were returning a stream/file in the method but we never closed it. So i guess while is opened we cannot read it.

def output_stream
  output_filename = @argv_options.output_filename
  output_filename.nil? ? $stdout : File.open(output_filename, "w")
end

everytime we used output_stream it will return a new file object so even if we do something like this:

output_stream.puts "hello world"
output_stream.close

that will not close the file created the first time when we do output_stream.puts "hello world"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried closing the output stream explicitly when we were handling output to a file as well as flushing the stream but couldn't make it work either way so I didn't think it had to do with that, very strange issue.

@manuca manuca self-assigned this Sep 28, 2020
if filename.nil?
$stdout.puts(message)
else
File.open(filename, "w") { |file| file.puts(message) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bronzdoc My only concern is that with this approach we're opening and closing immediately the file for each line we output.

Copy link
Contributor

Choose a reason for hiding this comment

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

We are not writing line by line we just open an IO object and write a complete string to it. But maybe I'm missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤔 I think you're right.

@bronzdoc bronzdoc requested a review from etagwerker September 29, 2020 18:49
Copy link
Member

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

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

@manuca Looks good! Could you add an entry to this file? https://github.com/fastruby/skunk/blob/main/CHANGELOG.md

Thanks!

@manuca
Copy link
Contributor Author

manuca commented Sep 29, 2020

@etagwerker added the entry to the CHANGELOG, I think it is ready for merging then.

@etagwerker etagwerker merged commit 7f8a482 into main Sep 29, 2020
@etagwerker etagwerker deleted the output-to-file branch September 29, 2020 21:47
@etagwerker
Copy link
Member

@manuca Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Output to file
3 participants