Skip to content

Commit

Permalink
Make --out to create parent directories
Browse files Browse the repository at this point in the history
  • Loading branch information
yous committed Oct 18, 2014
1 parent b2a1d46 commit f085e2a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2014-10-12 09:19:35 +0200 using RuboCop version 0.26.1.
# on 2014-10-17 19:20:51 +0900 using RuboCop version 0.26.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -18,7 +18,7 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 135
# Offense count: 136
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 21
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [#1350](https://github.com/bbatsov/rubocop/issues/1350): Guard against `Blocks` cop introducing syntax errors in auto-correct. ([@jonas054][])
* [#1374](https://github.com/bbatsov/rubocop/issues/1374): To eliminate interference, auto-correction is now done by one cop at a time, with saving and re-parsing inbetween. ([@jonas054][])
* [#1388](https://github.com/bbatsov/rubocop/issues/1388): Fix a false positive in `FormatString`. ([@bbatsov][])
* [#1389](https://github.com/bbatsov/rubocop/issues/1389): Make `--out` to create parent directories. ([@yous][])

## 0.26.1 (18/09/2014)

Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/formatter/formatter_set.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# encoding: utf-8

require 'fileutils'

module RuboCop
module Formatter
# This is a collection of formatters. A FormatterSet can hold multiple
Expand Down Expand Up @@ -37,7 +39,13 @@ def add_formatter(formatter_type, output_path = nil)
builtin_formatter_class(formatter_type)
end

output = output_path ? File.open(output_path, 'w') : $stdout
if output_path
dir_path = File.dirname(output_path)
FileUtils.mkdir_p(dir_path) unless File.exist?(dir_path)
output = File.open(output_path, 'w')
else
output = $stdout
end

self << formatter_class.new(output)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/formatter/formatter_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ module Formatter
expect(formatter_set.first.output.class).to eq(File)
expect(formatter_set.first.output.path).to eq(output_path)
end

it "creates parent directories if they don't exist" do
Dir.mktmpdir do |tmpdir|
output_path = File.join(tmpdir, 'path/does/not/exist')
formatter_set.add_formatter('simple', output_path)
expect(formatter_set.first.output.class).to eq(File)
expect(formatter_set.first.output.path).to eq(output_path)
end
end
end
end

Expand Down

0 comments on commit f085e2a

Please sign in to comment.