scripto 0.0.4 → 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_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)
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'.freeze
7
- FAILS = 'cat scripto_bogus_file 2> /dev/null'.freeze
8
- BAD_COMMAND = 'this_command_doesnt_exist'.freeze
9
- SRC = '_scripto_src'.freeze
10
- DST = '_scripto with spaces'.freeze
11
- ARGS = [ '-f', SRC, DST ].freeze
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
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