scripto 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,4 +15,4 @@ module Scripto
15
15
  self.verbose = options[:verbose]
16
16
  end
17
17
  end
18
- end
18
+ end
@@ -1,9 +1,9 @@
1
- require "digest/md5"
2
- require "etc"
1
+ require 'digest/md5'
2
+ require 'etc'
3
3
 
4
4
  module Scripto
5
5
  module MiscCommands
6
- BASE_62 = ("0".."9").to_a + ("A".."Z").to_a + ("a".."z").to_a
6
+ BASE_62 = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
7
7
 
8
8
  # Who is the current user?
9
9
  def whoami
@@ -12,16 +12,14 @@ module Scripto
12
12
 
13
13
  # Return true if the current user is "root".
14
14
  def root?
15
- whoami == "root"
15
+ whoami == 'root'
16
16
  end
17
17
 
18
18
  # Return the md5 checksum for the file at +path+.
19
19
  def md5_file(path)
20
20
  File.open(path) do |f|
21
- digest, buf = Digest::MD5.new, ""
22
- while f.read(4096, buf)
23
- digest.update(buf)
24
- end
21
+ digest, buf = Digest::MD5.new, ''
22
+ digest.update(buf) while f.read(4096, buf)
25
23
  digest.hexdigest
26
24
  end
27
25
  end
@@ -44,4 +42,4 @@ module Scripto
44
42
  (1..len).map { BASE_62.sample }.join
45
43
  end
46
44
  end
47
- end
45
+ end
@@ -1,9 +1,9 @@
1
1
  module Scripto
2
2
  module PrintCommands
3
- RESET = "\e[0m"
4
- GREEN = "\e[1;37;42m"
5
- YELLOW = "\e[1;37;43m"
6
- RED = "\e[1;37;41m"
3
+ RESET = "\e[0m".freeze
4
+ GREEN = "\e[1;37;42m".freeze
5
+ YELLOW = "\e[1;37;43m".freeze
6
+ RED = "\e[1;37;41m".freeze
7
7
 
8
8
  attr_accessor :verbose
9
9
 
@@ -35,8 +35,8 @@ module Scripto
35
35
 
36
36
  # Print a colored banner to $stderr in green.
37
37
  def banner(str, color: GREEN)
38
- now = Time.new.strftime("%H:%M:%S")
39
- s = "#{str} ".ljust(72, " ")
38
+ now = Time.new.strftime('%H:%M:%S')
39
+ s = "#{str} ".ljust(72, ' ')
40
40
  $stderr.puts "#{color}[#{now}] #{s}#{RESET}"
41
41
  end
42
42
 
@@ -51,4 +51,4 @@ module Scripto
51
51
  exit(1)
52
52
  end
53
53
  end
54
- end
54
+ end
@@ -1,5 +1,5 @@
1
- require "English"
2
- require "shellwords"
1
+ require 'English'
2
+ require 'shellwords'
3
3
 
4
4
  module Scripto
5
5
  module RunCommands
@@ -34,12 +34,10 @@ module Scripto
34
34
 
35
35
  # Returns true if the command succeeds. See #run for details.
36
36
  def run_succeeds?(command, args = nil)
37
- begin
38
- run_quietly(command, args)
39
- true
40
- rescue Error
41
- false
42
- end
37
+ run_quietly(command, args)
38
+ true
39
+ rescue Error
40
+ false
43
41
  end
44
42
 
45
43
  # Returns true if the command fails. See #run for details.
@@ -52,18 +50,18 @@ module Scripto
52
50
  Shellwords.escape(str)
53
51
  end
54
52
 
55
- protected
56
-
57
53
  # :nodoc:
58
54
  class CommandLine
59
55
  attr_accessor :command, :args
60
56
 
61
57
  def initialize(command, args)
62
58
  self.command = command
63
- self.args = if args
64
- args.map(&:to_s)
65
- else
66
- [ ]
59
+ self.args = begin
60
+ if args
61
+ args.map(&:to_s)
62
+ else
63
+ []
64
+ end
67
65
  end
68
66
  end
69
67
 
@@ -74,7 +72,7 @@ module Scripto
74
72
 
75
73
  def capture
76
74
  begin
77
- captured = `#{to_s}`
75
+ captured = `#{self}`
78
76
  rescue Errno::ENOENT
79
77
  raise Error, "#{self} failed : ENOENT (No such file or directory)"
80
78
  end
@@ -83,20 +81,21 @@ module Scripto
83
81
  end
84
82
 
85
83
  def raise!(status)
86
- if status.termsig == Signal.list["INT"]
84
+ if status.termsig == Signal.list['INT']
87
85
  raise "#{self} interrupted"
88
86
  end
87
+
89
88
  raise Error, "#{self} failed : #{status.to_i / 256}"
90
89
  end
91
90
 
92
91
  def to_s
93
- if args.length > 0
92
+ if !args.empty?
94
93
  escaped = args.map { |i| Shellwords.escape(i) }
95
- "#{command} #{escaped.join(" ")}"
94
+ "#{command} #{escaped.join(' ')}"
96
95
  else
97
96
  command
98
97
  end
99
98
  end
100
99
  end
101
100
  end
102
- end
101
+ end
@@ -1,4 +1,4 @@
1
1
  module Scripto
2
2
  # current Scripto version
3
- VERSION = "0.0.3"
3
+ VERSION = '0.0.4'.freeze
4
4
  end
@@ -1,25 +1,25 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'scripto/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "scripto"
6
+ spec.name = 'scripto'
8
7
  spec.version = Scripto::VERSION
9
8
  spec.platform = Gem::Platform::RUBY
10
- spec.required_ruby_version = ">= 2.0.0"
11
- spec.authors = ["Adam Doppelt"]
12
- spec.email = ["amd@gurge.com"]
13
- spec.summary = "Helpers for writing command line scripts. An extraction from Dwellable."
14
- spec.homepage = "http://github.com/gurgeous/scripto"
15
- spec.license = "MIT"
9
+ spec.required_ruby_version = '>= 2.0.0'
10
+ spec.authors = [ 'Adam Doppelt' ]
11
+ spec.email = [ 'amd@gurge.com' ]
12
+ spec.summary = 'Helpers for writing command line scripts. An extraction from Dwellable.'
13
+ spec.homepage = 'http://github.com/gurgeous/scripto'
14
+ spec.license = 'MIT'
16
15
 
17
16
  spec.files = `git ls-files -z`.split("\x0")
18
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
- spec.require_paths = ["lib"]
19
+ spec.require_paths = [ 'lib' ]
21
20
 
22
- spec.add_development_dependency "bundler", "~> 1.5"
23
- spec.add_development_dependency "minitest", "~> 5.0"
24
- spec.add_development_dependency "rake"
21
+ spec.add_development_dependency 'bundler', '~> 2.1'
22
+ spec.add_development_dependency 'minitest', '~> 5.14'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rubocop'
25
25
  end
@@ -1,12 +1,12 @@
1
- require "fileutils"
2
- require "minitest/autorun"
3
- require "minitest/pride"
1
+ require 'fileutils'
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
4
 
5
- $LOAD_PATH << File.expand_path("../../lib", __FILE__)
6
- require "scripto"
5
+ $LOAD_PATH << File.expand_path('../lib', __dir__)
6
+ require 'scripto'
7
7
 
8
8
  module Helper
9
- TMP_DIR = "/tmp/_scripto_test"
9
+ TMP_DIR = '/tmp/_scripto_test'.freeze
10
10
 
11
11
  # attr_accessor :main
12
12
 
@@ -23,4 +23,4 @@ module Helper
23
23
  FileUtils.rm_rf(TMP_DIR)
24
24
  Dir.chdir(@pwd)
25
25
  end
26
- end
26
+ end
@@ -1,13 +1,13 @@
1
- require_relative "helper"
1
+ require_relative 'helper'
2
2
 
3
3
  class TestCsv < Minitest::Test
4
4
  include Helper
5
5
 
6
- FILE = "test.csv"
6
+ FILE = 'test.csv'.freeze
7
7
 
8
- ROWS = [ { a: "1", b: "2", c: "3" }, { a: "4", b: "5", c: "6" } ]
9
- ROWS_EXP = "a,b,c\n1,2,3\n4,5,6\n"
10
- ROWS_REVERSED_EXP = "c,b,a\n3,2,1\n6,5,4\n"
8
+ ROWS = [ { a: '1', b: '2', c: '3' }, { a: '4', b: '5', c: '6' } ].freeze
9
+ ROWS_EXP = "a,b,c\n1,2,3\n4,5,6\n".freeze
10
+ ROWS_REVERSED_EXP = "c,b,a\n3,2,1\n6,5,4\n".freeze
11
11
 
12
12
  def test_types
13
13
  assert_equal(ROWS_EXP, Scripto.csv_to_s(ROWS))
@@ -16,9 +16,9 @@ class TestCsv < Minitest::Test
16
16
  end
17
17
 
18
18
  def test_cols
19
- assert_equal(ROWS_REVERSED_EXP, Scripto.csv_to_s(ROWS, cols: %i(c b a)))
20
- assert_equal(ROWS_REVERSED_EXP, Scripto.csv_to_s(structs, cols: %i(c b a)))
21
- assert_equal(ROWS_REVERSED_EXP, Scripto.csv_to_s(ostructs, cols: %i(c b a)))
19
+ assert_equal(ROWS_REVERSED_EXP, Scripto.csv_to_s(ROWS, cols: %i[c b a]))
20
+ assert_equal(ROWS_REVERSED_EXP, Scripto.csv_to_s(structs, cols: %i[c b a]))
21
+ assert_equal(ROWS_REVERSED_EXP, Scripto.csv_to_s(ostructs, cols: %i[c b a]))
22
22
  end
23
23
 
24
24
  def test_csv_stdout
@@ -37,14 +37,15 @@ class TestCsv < Minitest::Test
37
37
  assert_equal(ROWS, Scripto.csv_read("#{FILE}.gz").map(&:to_h))
38
38
  end
39
39
 
40
- def test_atomic
41
- magic = "can't touch this"
42
- File.write(FILE, magic)
43
- begin
44
- Util.csv_write(TMP, rows, cols: %i(bad_column))
45
- rescue
46
- end
47
- assert_equal(magic, File.read(FILE))
40
+ def test_bom
41
+ assert_equal('apple', Scripto.csv_read(write_bom).first.fruit)
42
+ end
43
+
44
+ def test_gz_with_bom
45
+ skip # this doesn't work yet
46
+ path = write_bom
47
+ Scripto.run("gzip #{path}")
48
+ assert_equal('apple', Scripto.csv_read("#{path}.gz").first.fruit)
48
49
  end
49
50
 
50
51
  protected
@@ -57,4 +58,13 @@ class TestCsv < Minitest::Test
57
58
  def ostructs
58
59
  ROWS.map { |i| OpenStruct.new(i) }
59
60
  end
60
- end
61
+
62
+ def write_bom
63
+ CSV.open(FILE, 'w') do |csv|
64
+ csv.to_io.write "\uFEFF" # bom
65
+ csv << %w[fruit]
66
+ csv << %w[apple]
67
+ end
68
+ FILE
69
+ end
70
+ end
@@ -1,16 +1,16 @@
1
- require_relative "helper"
1
+ require_relative 'helper'
2
2
 
3
3
  class TestFile < Minitest::Test
4
4
  include Helper
5
5
 
6
- DIR = "dir"
7
- SRC, DST = "src.txt", "dst.txt"
8
- SRC2, DST2 = "src2.txt", "dst2.txt"
6
+ DIR = 'dir'.freeze
7
+ SRC, DST = 'src.txt', 'dst.txt'
8
+ SRC2, DST2 = 'src2.txt', 'dst2.txt'
9
9
 
10
10
  def setup
11
11
  super
12
- File.write(SRC, "something")
13
- File.write(SRC2, "another thing")
12
+ File.write(SRC, 'something')
13
+ File.write(SRC2, 'another thing')
14
14
  end
15
15
 
16
16
  #
@@ -23,8 +23,8 @@ class TestFile < Minitest::Test
23
23
  end
24
24
 
25
25
  def test_mkdir_mode
26
- Scripto.mkdir(DIR, mode: 0644)
27
- assert_equal(0644, File.stat(DIR).mode & 0644)
26
+ Scripto.mkdir(DIR, mode: 0o644)
27
+ assert_equal(0o644, File.stat(DIR).mode & 0o644)
28
28
  end
29
29
 
30
30
  def test_cp
@@ -33,8 +33,8 @@ class TestFile < Minitest::Test
33
33
  end
34
34
 
35
35
  def test_cp_mode
36
- Scripto.cp(SRC, DST, mode: 0644)
37
- assert_equal(0644, File.stat(DST).mode & 0644)
36
+ Scripto.cp(SRC, DST, mode: 0o644)
37
+ assert_equal(0o644, File.stat(DST).mode & 0o644)
38
38
  end
39
39
 
40
40
  def test_cp_mkdir
@@ -45,13 +45,13 @@ class TestFile < Minitest::Test
45
45
 
46
46
  def test_mv
47
47
  Scripto.mv(SRC, DST)
48
- assert_equal("something", File.read(DST))
48
+ assert_equal('something', File.read(DST))
49
49
  end
50
50
 
51
51
  def test_mv_mkdir
52
52
  in_dir = "#{DIR}/in_dir"
53
53
  Scripto.mv(SRC, in_dir, mkdir: true)
54
- assert_equal("something", File.read(in_dir))
54
+ assert_equal('something', File.read(in_dir))
55
55
  end
56
56
 
57
57
  def test_ln
@@ -62,7 +62,7 @@ class TestFile < Minitest::Test
62
62
  def test_rm
63
63
  Scripto.rm(SRC)
64
64
  assert(!File.exist?(SRC))
65
- Scripto.rm("this_file_doesnt_exist") # shouldn't complain
65
+ Scripto.rm('this_file_doesnt_exist') # shouldn't complain
66
66
  end
67
67
 
68
68
  #
@@ -80,7 +80,7 @@ class TestFile < Minitest::Test
80
80
  assert_equal(SRC, File.readlink(DST2))
81
81
 
82
82
  # should be silent
83
- assert_fu_output(nil, "") do
83
+ assert_fu_output(nil, '') do
84
84
  assert_nil Scripto.mkdir_if_necessary(DIR)
85
85
  assert_nil Scripto.cp_if_necessary(SRC, DST)
86
86
  assert_nil Scripto.ln_if_necessary(SRC, DST2)
@@ -88,7 +88,7 @@ class TestFile < Minitest::Test
88
88
  end
89
89
 
90
90
  def test_cp_if_necessary_differs
91
- File.write(DST, "this is different")
91
+ File.write(DST, 'this is different')
92
92
  assert_fu_output(nil, "cp -rp #{SRC} #{DST}\n") do
93
93
  assert Scripto.cp_if_necessary(SRC, DST)
94
94
  end
@@ -103,23 +103,23 @@ class TestFile < Minitest::Test
103
103
  end
104
104
 
105
105
  def test_chmod
106
- Scripto.chmod(SRC, 0644)
107
- assert_equal(0644, File.stat(SRC).mode & 0644)
106
+ Scripto.chmod(SRC, 0o644)
107
+ assert_equal(0o644, File.stat(SRC).mode & 0o644)
108
108
  end
109
109
 
110
110
  def test_rm_and_mkdir
111
111
  Dir.mkdir(DIR)
112
- File.write("#{DIR}/file", "this is a test")
112
+ File.write("#{DIR}/file", 'this is a test')
113
113
  assert Dir["#{DIR}/*"].length == 1
114
114
  Scripto.rm_and_mkdir(DIR)
115
- assert Dir["#{DIR}/*"].length == 0
115
+ assert Dir["#{DIR}/*"].empty?
116
116
  end
117
117
 
118
118
  def test_copy_metadata
119
- File.chmod(0644, SRC)
119
+ File.chmod(0o644, SRC)
120
120
  File.utime(1234, 5678, SRC)
121
121
  Scripto.copy_metadata(SRC, SRC2)
122
- assert_equal(0644, File.stat(SRC2).mode & 0644)
122
+ assert_equal(0o644, File.stat(SRC2).mode & 0o644)
123
123
  assert_equal(1234, File.stat(SRC2).atime.to_i)
124
124
  assert_equal(5678, File.stat(SRC2).mtime.to_i)
125
125
  end
@@ -130,20 +130,40 @@ class TestFile < Minitest::Test
130
130
 
131
131
  def test_mkdir_owner
132
132
  skip if !root?
133
- Scripto.mkdir(DIR, owner: "nobody")
134
- assert(Etc.getpwnam("nobody").uid, File.stat(DIR).uid)
133
+ Scripto.mkdir(DIR, owner: 'nobody')
134
+ assert(Etc.getpwnam('nobody').uid, File.stat(DIR).uid)
135
135
  end
136
136
 
137
137
  def test_cp_owner
138
138
  skip if !root?
139
- Scripto.cp(SRC, DST, owner: "nobody")
140
- assert(Etc.getpwnam("nobody").uid, File.stat(DST).uid)
139
+ Scripto.cp(SRC, DST, owner: 'nobody')
140
+ assert(Etc.getpwnam('nobody').uid, File.stat(DST).uid)
141
141
  end
142
142
 
143
143
  def test_chown
144
144
  skip if !root?
145
- Scripto.chown(SRC, "nobody")
146
- assert(Etc.getpwnam("nobody").uid, File.stat(SRC).uid)
145
+ Scripto.chown(SRC, 'nobody')
146
+ assert(Etc.getpwnam('nobody').uid, File.stat(SRC).uid)
147
+ end
148
+
149
+ #
150
+ # atomic
151
+ #
152
+
153
+ def test_atomic
154
+ Scripto.atomic_write(SRC) { |f| f.write('xyzzy') }
155
+ assert_equal 'xyzzy', File.read(SRC)
156
+ end
157
+
158
+ def test_atomic_fail
159
+ assert_raises do
160
+ Scripto.atomic_write(SRC) do |f|
161
+ f.write('xyzzy')
162
+ raise 'uh oh'
163
+ end
164
+ end
165
+ # SRC should be unscathed
166
+ assert_equal 'something', File.read(SRC)
147
167
  end
148
168
 
149
169
  #
@@ -184,17 +204,17 @@ class TestFile < Minitest::Test
184
204
 
185
205
  def root?
186
206
  if !defined?(@root)
187
- @root = `whoami`.strip == "root"
207
+ @root = `whoami`.strip == 'root'
188
208
  end
189
209
  @root
190
210
  end
191
211
 
192
- def assert_fu_output(stdout = nil, stderr = nil, &block)
212
+ def assert_fu_output(stdout = nil, stderr = nil)
193
213
  Scripto.verbose!
194
214
  assert_output(stdout, stderr) do
195
215
  # FileUtils squirrels this away so we have to set it manually
196
- FileUtils.instance_eval("@fileutils_output = $stderr")
216
+ FileUtils.instance_eval('@fileutils_output = $stderr')
197
217
  yield
198
218
  end
199
219
  end
200
- end
220
+ end