scripto 0.0.3 → 1.0.0

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.
data/test/test_file.rb DELETED
@@ -1,200 +0,0 @@
1
- require_relative "helper"
2
-
3
- class TestFile < Minitest::Test
4
- include Helper
5
-
6
- DIR = "dir"
7
- SRC, DST = "src.txt", "dst.txt"
8
- SRC2, DST2 = "src2.txt", "dst2.txt"
9
-
10
- def setup
11
- super
12
- File.write(SRC, "something")
13
- File.write(SRC2, "another thing")
14
- end
15
-
16
- #
17
- # basics
18
- #
19
-
20
- def test_mkdir
21
- Scripto.mkdir(DIR)
22
- assert(File.directory?(DIR))
23
- end
24
-
25
- def test_mkdir_mode
26
- Scripto.mkdir(DIR, mode: 0644)
27
- assert_equal(0644, File.stat(DIR).mode & 0644)
28
- end
29
-
30
- def test_cp
31
- Scripto.cp(SRC, DST)
32
- assert_equal(File.read(SRC), File.read(DST))
33
- end
34
-
35
- def test_cp_mode
36
- Scripto.cp(SRC, DST, mode: 0644)
37
- assert_equal(0644, File.stat(DST).mode & 0644)
38
- end
39
-
40
- def test_cp_mkdir
41
- in_dir = "#{DIR}/in_dir"
42
- Scripto.cp(SRC, in_dir, mkdir: true)
43
- assert_equal(File.read(SRC), File.read(in_dir))
44
- end
45
-
46
- def test_mv
47
- Scripto.mv(SRC, DST)
48
- assert_equal("something", File.read(DST))
49
- end
50
-
51
- def test_mv_mkdir
52
- in_dir = "#{DIR}/in_dir"
53
- Scripto.mv(SRC, in_dir, mkdir: true)
54
- assert_equal("something", File.read(in_dir))
55
- end
56
-
57
- def test_ln
58
- Scripto.ln(SRC, DST)
59
- assert_equal(SRC, File.readlink(DST))
60
- end
61
-
62
- def test_rm
63
- Scripto.rm(SRC)
64
- assert(!File.exist?(SRC))
65
- Scripto.rm("this_file_doesnt_exist") # shouldn't complain
66
- end
67
-
68
- #
69
- # if necessary (this is useful for printing)
70
- #
71
-
72
- def test_if_necessary
73
- # should return true
74
- assert Scripto.mkdir_if_necessary(DIR)
75
- assert Scripto.cp_if_necessary(SRC, DST)
76
- assert Scripto.ln_if_necessary(SRC, DST2)
77
-
78
- assert(File.directory?(DIR))
79
- assert_equal(File.read(SRC), File.read(DST))
80
- assert_equal(SRC, File.readlink(DST2))
81
-
82
- # should be silent
83
- assert_fu_output(nil, "") do
84
- assert_nil Scripto.mkdir_if_necessary(DIR)
85
- assert_nil Scripto.cp_if_necessary(SRC, DST)
86
- assert_nil Scripto.ln_if_necessary(SRC, DST2)
87
- end
88
- end
89
-
90
- def test_cp_if_necessary_differs
91
- File.write(DST, "this is different")
92
- assert_fu_output(nil, "cp -rp #{SRC} #{DST}\n") do
93
- assert Scripto.cp_if_necessary(SRC, DST)
94
- end
95
- end
96
-
97
- def test_ln_if_necessary_differs
98
- File.symlink(SRC2, DST)
99
- assert_fu_output(nil, "rm -f #{DST}\nln -sf #{SRC} #{DST}\n") do
100
- assert Scripto.ln_if_necessary(SRC, DST)
101
- assert_equal(SRC, File.readlink(DST))
102
- end
103
- end
104
-
105
- def test_chmod
106
- Scripto.chmod(SRC, 0644)
107
- assert_equal(0644, File.stat(SRC).mode & 0644)
108
- end
109
-
110
- def test_rm_and_mkdir
111
- Dir.mkdir(DIR)
112
- File.write("#{DIR}/file", "this is a test")
113
- assert Dir["#{DIR}/*"].length == 1
114
- Scripto.rm_and_mkdir(DIR)
115
- assert Dir["#{DIR}/*"].length == 0
116
- end
117
-
118
- def test_copy_metadata
119
- File.chmod(0644, SRC)
120
- File.utime(1234, 5678, SRC)
121
- Scripto.copy_metadata(SRC, SRC2)
122
- assert_equal(0644, File.stat(SRC2).mode & 0644)
123
- assert_equal(1234, File.stat(SRC2).atime.to_i)
124
- assert_equal(5678, File.stat(SRC2).mtime.to_i)
125
- end
126
-
127
- #
128
- # chown rests - must be root
129
- #
130
-
131
- def test_mkdir_owner
132
- skip if !root?
133
- Scripto.mkdir(DIR, owner: "nobody")
134
- assert(Etc.getpwnam("nobody").uid, File.stat(DIR).uid)
135
- end
136
-
137
- def test_cp_owner
138
- skip if !root?
139
- Scripto.cp(SRC, DST, owner: "nobody")
140
- assert(Etc.getpwnam("nobody").uid, File.stat(DST).uid)
141
- end
142
-
143
- def test_chown
144
- skip if !root?
145
- Scripto.chown(SRC, "nobody")
146
- assert(Etc.getpwnam("nobody").uid, File.stat(SRC).uid)
147
- end
148
-
149
- #
150
- # verbosity
151
- #
152
-
153
- def test_mkdir_verbose
154
- assert_fu_output(nil, "mkdir -p #{DIR}\n") do
155
- Scripto.mkdir(DIR)
156
- end
157
- end
158
-
159
- def test_cp_verbose
160
- assert_fu_output(nil, "cp -rp #{SRC} #{DST}\n") do
161
- Scripto.cp(SRC, DST)
162
- end
163
- end
164
-
165
- def test_mv_verbose
166
- assert_fu_output(nil, "mv #{SRC} #{DST}\n") do
167
- Scripto.mv(SRC, DST)
168
- end
169
- end
170
-
171
- def test_ln_verbose
172
- assert_fu_output(nil, "ln -sf #{SRC} #{DST}\n") do
173
- Scripto.ln(SRC, DST)
174
- end
175
- end
176
-
177
- def test_rm_verbose
178
- assert_fu_output(nil, "rm -f #{SRC}\n") do
179
- Scripto.rm(SRC)
180
- end
181
- end
182
-
183
- protected
184
-
185
- def root?
186
- if !defined?(@root)
187
- @root = `whoami`.strip == "root"
188
- end
189
- @root
190
- end
191
-
192
- def assert_fu_output(stdout = nil, stderr = nil, &block)
193
- Scripto.verbose!
194
- assert_output(stdout, stderr) do
195
- # FileUtils squirrels this away so we have to set it manually
196
- FileUtils.instance_eval("@fileutils_output = $stderr")
197
- yield
198
- end
199
- end
200
- end
data/test/test_misc.rb DELETED
@@ -1,53 +0,0 @@
1
- require_relative "helper"
2
-
3
- class TestMisc < Minitest::Test
4
- include Helper
5
-
6
- def test_whoami
7
- assert_equal(`whoami`.strip, Scripto.whoami)
8
- end
9
-
10
- def test_root?
11
- assert_equal(`whoami`.strip == "root", Scripto.root?)
12
- end
13
-
14
- def test_md5_string
15
- assert_equal("ba73632f801ac2c72d78134722f2cb84", Scripto.md5_string("gub"))
16
- end
17
-
18
- def test_md5_file
19
- File.write("test.txt", "gub")
20
- assert_equal("ba73632f801ac2c72d78134722f2cb84", Scripto.md5_file("test.txt"))
21
- end
22
-
23
- def test_prompt?
24
- with_fake_stdin("YES") do
25
- assert_output(nil, "question (y/n) ") do
26
- assert Scripto.prompt?("question")
27
- end
28
- end
29
- with_fake_stdin("y") do
30
- assert_output(nil, "question (y/n) ") do
31
- assert Scripto.prompt?("question")
32
- end
33
- end
34
- with_fake_stdin("no") do
35
- assert_output(nil, "question (y/n) ") do
36
- assert !Scripto.prompt?("question")
37
- end
38
- end
39
- end
40
-
41
- protected
42
-
43
- def with_fake_stdin(str, &block)
44
- old_stdin = $stdin
45
- begin
46
- $stdin = StringIO.new(str)
47
- $stdin.rewind
48
- yield
49
- ensure
50
- $stdin = old_stdin
51
- end
52
- end
53
- end
data/test/test_print.rb DELETED
@@ -1,28 +0,0 @@
1
- require_relative "helper"
2
-
3
- class TestPrint < Minitest::Test
4
- include Helper
5
-
6
- def test_verbose?
7
- assert(!Scripto.verbose?)
8
- Scripto.verbose!
9
- assert(Scripto.verbose?)
10
- end
11
-
12
- def test_quiet
13
- assert_silent { Scripto.vbanner "gub" }
14
- assert_silent { Scripto.vprintf("gub %d", 123) }
15
- assert_silent { Scripto.vputs "gub" }
16
- end
17
-
18
- def test_loud
19
- Scripto.verbose!
20
- assert_output(nil, /gub/) { Scripto.vbanner "gub" }
21
- assert_output(nil, /gub/) { Scripto.vprintf("zub %s", "gub") }
22
- assert_output(nil, /gub/) { Scripto.vputs "gub" }
23
- end
24
-
25
- def test_warning
26
- assert_output(nil, /gub/) { Scripto.warning "gub" }
27
- end
28
- end
data/test/test_run.rb DELETED
@@ -1,103 +0,0 @@
1
- require_relative "helper"
2
-
3
- class TestRun < Minitest::Test
4
- include Helper
5
-
6
- SUCCEEDS = "echo gub"
7
- FAILS = "cat scripto_bogus_file 2> /dev/null"
8
- BAD_COMMAND = "this_command_doesnt_exist"
9
- SRC = "_scripto_src"
10
- DST = "_scripto with spaces"
11
- ARGS = [ "-f", SRC, DST ]
12
-
13
- def setup
14
- super
15
- File.write(SRC, "something")
16
- end
17
-
18
- #
19
- # basic methods
20
- #
21
-
22
- def test_run
23
- Scripto.run("#{SUCCEEDS} > #{SRC}")
24
- assert_equal("gub", File.read(SRC).strip)
25
- assert("gub", Scripto.run_capture(SUCCEEDS))
26
- Scripto.run_quietly(SUCCEEDS)
27
- end
28
-
29
- def test_run_succeeds?
30
- assert_succeeds(SUCCEEDS)
31
- assert_fails(FAILS)
32
- assert_fails(BAD_COMMAND)
33
- end
34
-
35
- def test_shellescape
36
- assert_equal("gub", Scripto.shellescape("gub"))
37
- assert_equal("gub\\ zub", Scripto.shellescape("gub zub"))
38
- end
39
-
40
- # verbosity
41
- def test_verbose
42
- Scripto.verbose!
43
- cmd = "#{SUCCEEDS} > /dev/null"
44
- assert_output(nil, "#{cmd}\n") do
45
- Scripto.run(cmd)
46
- end
47
- end
48
-
49
- # commands that fail
50
- def test_failures
51
- assert_raises(Scripto::RunCommands::Error) { Scripto.run(BAD_COMMAND) }
52
- assert_raises(Scripto::RunCommands::Error) { Scripto.run_capture(BAD_COMMAND) }
53
- assert_raises(Scripto::RunCommands::Error) { Scripto.run(FAILS) }
54
- assert_raises(Scripto::RunCommands::Error) { Scripto.run_capture(FAILS) }
55
- end
56
-
57
- #
58
- # args
59
- #
60
-
61
- def test_run_args
62
- # make sure SRC is copied to DST in all cases
63
- assert_cp { Scripto.run("cp", ARGS) }
64
- assert_cp { Scripto.run_quietly("cp", ARGS) }
65
- assert_cp { assert_succeeds("cp", ARGS) }
66
- end
67
-
68
- def test_capture_escaping
69
- tricky = "\"'!tricky!'\""
70
- assert_equal("#{tricky} #{tricky}\n", Scripto.run_capture("echo", [tricky, tricky]))
71
- end
72
-
73
- def test_args_succeeds_fails
74
- assert_fails(BAD_COMMAND, ARGS)
75
- end
76
-
77
- # is output escaped properly with verbose?
78
- def test_args_verbose
79
- Scripto.verbose!
80
- assert_output(nil, "cp -f #{SRC} #{DST.gsub(" ", "\\ ")}\n") do
81
- Scripto.run("cp", ARGS)
82
- end
83
- end
84
-
85
- protected
86
-
87
- def assert_cp(&block)
88
- File.unlink(DST) if File.exist?(DST)
89
- yield
90
- assert_equal(File.read(SRC), File.read(DST))
91
- File.unlink(DST)
92
- end
93
-
94
- def assert_succeeds(command, args = nil)
95
- assert_equal(true, Scripto.run_succeeds?(command, args))
96
- assert_equal(false, Scripto.run_fails?(command, args))
97
- end
98
-
99
- def assert_fails(command, args = nil)
100
- assert_equal(false, Scripto.run_succeeds?(command, args))
101
- assert_equal(true, Scripto.run_fails?(command, args))
102
- end
103
- end
data/vagrant_provision DELETED
@@ -1,50 +0,0 @@
1
- #!/bin/bash
2
-
3
- # bail on errors
4
- set -eu
5
-
6
- function banner() {
7
- printf '\e[1;37;43m[%s] %-72s\e[0m\n' `date '+%H:%M:%S'` "vagrant_provision: $1"
8
- }
9
-
10
- # packages needed for building ruby
11
- banner "Packages..."
12
- sudo apt-get -y install git-core
13
- sudo apt-get -y install build-essential checkinstall libffi-dev libreadline-gplv2-dev libncurses5-dev libssl-dev libyaml-dev zlib1g-dev
14
-
15
- # rbenv
16
- banner "Rbenv..."
17
- if [ ! -d ~/.rbenv ] ; then
18
- git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
19
- fi
20
- if [ ! -d ~/.rbenv/plugins/ruby-build ] ; then
21
- git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
22
- fi
23
-
24
- # add rbenv to .bashrc
25
- if ! grep -q 'vagrant provisioning' ~/.bashrc ; then
26
- cat >> ~/.bashrc <<"EOF"
27
- # added by vagrant provisioning
28
- export PATH="$HOME/.rbenv/bin:$PATH"
29
- eval "$(rbenv init -)"
30
- EOF
31
-
32
- # make sure we pickup rbenv too!
33
- export PATH="$HOME/.rbenv/bin:$PATH"
34
- eval "$(rbenv init -)"
35
- fi
36
-
37
- # no rdoc, please
38
- echo 'gem: --no-ri --no-rdoc' > ~/.gemrc
39
-
40
- # now install ruby
41
- banner "Ruby..."
42
- rbenv install 2.0.0-p353
43
- rbenv global 2.0.0-p353
44
- ruby -v
45
-
46
- # and gems
47
- banner "Gems..."
48
- gem update --system
49
- gem install bundler
50
- rbenv rehash