sugar_utils 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1742a971ec43f0580b8a66eb40a6068f0bd70a5
4
- data.tar.gz: 35a2e24ceb3eeac3109f9e9d300c20ea7a59ffe6
3
+ metadata.gz: 6e0e229f79ac2862d3410a0af00c9193c63e95bc
4
+ data.tar.gz: db296249a7db975a57b0931ae02bd2f5f3e7339c
5
5
  SHA512:
6
- metadata.gz: 82dd25fd3f8de787bf325343a948f44bc6f539d65b5b29f2476ae3f24fe235422159129184998cafc0505597a215e69d47a725e17e60ed8f40068de84090f4e6
7
- data.tar.gz: 95b3f9228923c4aad53fb11d4d7a42a31b8ac832480aac4dc338f6859006f596765e9e8f5f853b053d83cb58afed9e19f2d6f68e682692b323f005deff94499a
6
+ metadata.gz: 12f986da3764bc8c2ef501a0f93eed04309f0a8d1063de8595a82af448e2d02fbe7ba89ebbcbd0e970a5e51af232e77366d3dc9827d8c260693dbf6a1599a2de
7
+ data.tar.gz: 24f18068100970ee7eff0fcf0d63d1aa8ab50dbf130e0f608d3367ca52f7a896a7d7a45e5b5f129ee6af712783eaac4d96c792eefb96715e949a7f96422f3ff3
data/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ## [0.4.1] - 2017-05-17
6
+ ### Added
7
+ - options :owner, :group, :mode to SugarUtils::File.write and .touch
8
+
9
+ ### Changed
10
+ - marked the :perm option as deprecated by :mode
11
+
5
12
  ## [0.4.0] - 2017-05-17
6
13
  ### Added
7
14
  - SugarUtils::File.touch, which will ensure the directory before touching the
@@ -74,11 +74,24 @@ module SugarUtils
74
74
  end
75
75
 
76
76
  # @param [String] filename
77
+ # @param [Hash] options
78
+ # @option options [String, Integer] :owner
79
+ # @option options [String, Integer] :group
80
+ # @option options [Integer] :mode
81
+ # @option options [Integer] :perm @deprecated
77
82
  #
78
83
  # @return [void]
79
- def self.touch(filename)
84
+ def self.touch(filename, options = {})
85
+ owner = options[:owner]
86
+ group = options[:group]
87
+ mode = options[:mode] || options[:perm]
88
+
89
+ deprecate_option(:touch, :perm, :mode, 2017, 8) if options.has_key?(:perm)
90
+
80
91
  FileUtils.mkdir_p(::File.dirname(filename))
81
92
  FileUtils.touch(filename)
93
+ FileUtils.chown(owner, group, filename)
94
+ FileUtils.chmod(mode, filename) if mode
82
95
  end
83
96
 
84
97
  # @param [String] filename
@@ -86,17 +99,24 @@ module SugarUtils
86
99
  # @param [Hash] options
87
100
  # @option options [Integer] :timeout (10)
88
101
  # @option options [Boolean] :flush (false)
89
- # @option options [Integer] :perm (0666)
102
+ # @option options [String, Integer] :owner
103
+ # @option options [String, Integer] :group
104
+ # @option options [Integer] :mode (0o666)
105
+ # @option options [Integer] :perm (0o666) @deprecated
90
106
  #
91
107
  # @raise [SugarUtils::File::Error]
92
108
  #
93
109
  # @return [void]
94
110
  def self.write(filename, data, options = {})
95
- perm = options[:perm] || 0o666
96
111
  flush = options[:flush] || false
112
+ owner = options[:owner]
113
+ group = options[:group]
114
+ mode = options[:mode] || options[:perm] || 0o666
115
+
116
+ deprecate_option(:touch, :perm, :mode, 2017, 8) if options.has_key?(:perm)
97
117
 
98
118
  FileUtils.mkdir_p(::File.dirname(filename))
99
- ::File.open(filename, ::File::RDWR | ::File::CREAT, perm) do |file|
119
+ ::File.open(filename, ::File::RDWR | ::File::CREAT, mode) do |file|
100
120
  flock_exclusive(file, options)
101
121
 
102
122
  file.truncate(0) # Ensure file is empty before proceeding.
@@ -112,8 +132,9 @@ module SugarUtils
112
132
  end
113
133
 
114
134
  # Ensure that the permissions are correct if the file already existed.
115
- file.chmod(perm)
135
+ file.chmod(mode)
116
136
  end
137
+ FileUtils.chown(owner, group, filename)
117
138
  rescue Timeout::Error
118
139
  raise(Error, "Unable to write #{filename} because it is locked")
119
140
  rescue SystemCallError, IOError => boom
@@ -133,5 +154,33 @@ module SugarUtils
133
154
  def self.write_json(filename, data, options = {})
134
155
  write(filename, MultiJson.dump(data, pretty: true), options)
135
156
  end
157
+
158
+ ############################################################################
159
+
160
+ # Following the same pattern as the existing stdlib method deprecation
161
+ # module.
162
+ # @see http://ruby-doc.org/stdlib-2.0.0/libdoc/rubygems/rdoc/Gem/Deprecate.html
163
+ def self.deprecate_option(_method, option_name, option_repl, year, month)
164
+ return if Gem::Deprecate.skip
165
+
166
+ klass = self.is_a?(Module)
167
+ target = klass ? "#{self}." : "#{self.class}#"
168
+
169
+ # Determine the method
170
+ method = caller_locations(1,1).first.label
171
+
172
+ # Determine the caller
173
+ external_caller = caller_locations(2,1).first
174
+ location_of_external_caller = "#{external_caller.absolute_path}:#{external_caller.lineno}"
175
+
176
+ msg = [
177
+ "NOTE: #{target}#{method} option :#{option_name} is deprecated",
178
+ option_repl == :none ? ' with no replacement' : "; use :#{option_repl} instead",
179
+ ". It will be removed on or after %4d-%02d-01." % [year, month],
180
+ "\n#{target}#{method} called from #{location_of_external_caller}"
181
+ ]
182
+ warn("#{msg.join}.")
183
+ end
184
+ private_class_method :deprecate_option
136
185
  end
137
186
  end
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module SugarUtils
4
- VERSION = '0.4.0'.freeze
4
+ VERSION = '0.4.1'.freeze
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,7 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
4
  require 'sugar_utils'
5
5
  require 'rspec/tabular'
6
6
  require 'fakefs/spec_helpers'
7
+ require 'etc'
7
8
  # HACK: including pp seems to resolve an error with FakeFS and File.read
8
9
  # This seems to be related to but not the same as the problem mentioned in the
9
10
  # README
@@ -46,8 +47,28 @@ RSpec::Matchers.define :have_file_permission do |expected|
46
47
  match do |actual|
47
48
  next false unless File.exist?(actual)
48
49
 
49
- @actual = format('%o', File.stat(filename).mode)
50
+ @actual = format('%o', File.stat(actual).mode)
50
51
  @expected = format('%o', expected)
51
52
  values_match?(@expected, @actual)
52
53
  end
53
54
  end
55
+
56
+ RSpec::Matchers.define :have_owner do |expected|
57
+ match do |actual|
58
+ next false unless File.exist?(actual)
59
+
60
+ @actual = Etc.getpwuid(File.stat(filename).uid).name
61
+ @expected = expected
62
+ values_match?(@expected, @actual)
63
+ end
64
+ end
65
+
66
+ RSpec::Matchers.define :have_group do |expected|
67
+ match do |actual|
68
+ next false unless File.exist?(actual)
69
+
70
+ @actual = Etc.getgrgid(File.stat(actual).gid).name
71
+ @expected = expected
72
+ values_match?(@expected, @actual)
73
+ end
74
+ end
@@ -98,23 +98,22 @@ describe SugarUtils::File do
98
98
  end
99
99
 
100
100
  describe '.touch', :fakefs do
101
- subject { described_class.touch(filename) }
101
+ subject { described_class.touch(filename, *options) }
102
102
 
103
- before { subject }
104
-
105
- context 'without path' do
106
- let(:filename) { 'filename' }
107
- it { expect(File.exist?(filename)).to eq(true) }
108
- end
103
+ let(:filename) { 'path1/path2/filename' }
109
104
 
110
- context 'with path' do
111
- let(:filename) { 'path1/path2/filename' }
112
- it { expect(File.exist?(filename)).to eq(true) }
113
- end
105
+ before { subject }
114
106
 
115
- context 'with absolute path' do
116
- let(:filename) { '/path1/path2/filename' }
117
- it { expect(File.exist?(filename)).to eq(true) }
107
+ inputs :options
108
+ specify_with([]) { expect(File.exist?(filename)).to eq(true) }
109
+ specify_with([{ owner: 'nobody' }]) { expect(filename).to have_owner('nobody') }
110
+ specify_with([{ group: 'nogroup' }]) { expect(filename).to have_group('nogroup') }
111
+ specify_with([{ mode: 0o600 }]) { expect(filename).to have_file_permission(0o100600) }
112
+ specify_with([{ perm: 0o600 }]) { expect(filename).to have_file_permission(0o100600) }
113
+ specify_with([{ owner: 'nobody', group: 'nogroup', mode: 0o600 }]) do
114
+ expect(filename).to have_owner('nobody')
115
+ expect(filename).to have_group('nogroup')
116
+ expect(filename).to have_file_permission(0o100600)
118
117
  end
119
118
  end
120
119
 
@@ -161,14 +160,25 @@ describe SugarUtils::File do
161
160
  specify { expect(filename).to have_file_permission(0o100666) }
162
161
  end
163
162
 
164
- context 'options' do
165
- let(:options) { { flush: true, perm: 0o600 } }
163
+ context 'with deprecated options' do
164
+ let(:options) { { perm: 0o600 } }
165
+ before { subject }
166
+ specify { expect(filename).to have_content(data) }
167
+ specify { expect(filename).to have_file_permission(0o100600) }
168
+ end
169
+
170
+ context 'with deprecated options' do
171
+ let(:options) do
172
+ { flush: true, owner: 'nobody', group: 'nogroup', mode: 0o600 }
173
+ end
166
174
  before do
167
175
  expect_any_instance_of(File).to receive(:flush)
168
176
  expect_any_instance_of(File).to receive(:fsync)
169
177
  subject
170
178
  end
171
179
  specify { expect(filename).to have_content(data) }
180
+ specify { expect(filename).to have_owner('nobody') }
181
+ specify { expect(filename).to have_group('nogroup') }
172
182
  specify { expect(filename).to have_file_permission(0o100600) }
173
183
  end
174
184
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugar_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Sullivan Cant