obfusk-util 0.0.1.SNAPSHOT.20130724235009 → 0.0.1.SNAPSHOT.20130725000153

Sign up to get free protection for your applications and to get access to all the features.
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ desc 'Run specs'
2
+ task :spec do
3
+ sh 'rspec -c'
4
+ end
5
+
6
+ desc 'Run specs verbosely'
7
+ task 'spec:verbose' do
8
+ sh 'rspec -cfd'
9
+ end
10
+
11
+ desc 'Run specs verbosely, view w/ less'
12
+ task 'spec:less' do
13
+ sh 'rspec -cfd --tty | less -R'
14
+ end
15
+
16
+ desc 'Generate docs'
17
+ task :docs do
18
+ sh 'yardoc | cat'
19
+ end
20
+
21
+ desc 'List undocumented objects'
22
+ task 'docs:undoc' do
23
+ sh 'yard stats --list-undoc'
24
+ end
25
+
26
+ desc 'Cleanup'
27
+ task :clean do
28
+ sh 'rm -rf .yardoc/ doc/ *.gem'
29
+ end
30
+
31
+ desc 'Build SNAPSHOT gem'
32
+ task :snapshot do
33
+ v = Time.new.strftime '%Y%m%d%H%M%S'
34
+ f = 'lib/obfusk/util/version.rb'
35
+ sh "sed -ri~ 's!(SNAPSHOT)!\\1.#{v}!' #{f}"
36
+ sh 'gem build obfusk-util.gemspec'
37
+ end
38
+
39
+ desc 'Undo SNAPSHOT gem'
40
+ task 'snapshot:undo' do
41
+ sh 'git checkout -- lib/obfusk/util/version.rb'
42
+ end
@@ -1,5 +1,5 @@
1
1
  # my namespace
2
2
  module Obfusk; module Util
3
- VERSION = '0.0.1.SNAPSHOT.20130724235009'
3
+ VERSION = '0.0.1.SNAPSHOT.20130725000153'
4
4
  DATE = '2013-07-24'
5
5
  end; end
data/obfusk-util.gemspec CHANGED
@@ -19,8 +19,9 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.license = 'GPLv2'
21
21
 
22
- s.files = %w{ README.md obfusk-util.gemspec }\
23
- + Dir['lib/**/*.rb']
22
+ s.files = %w{ .yardopts README.md Rakefile } \
23
+ + %w{ obfusk-util.gemspec } \
24
+ + Dir['{lib,spec}/**/*.rb']
24
25
 
25
26
  s.add_development_dependency 'rake'
26
27
  s.add_development_dependency 'rspec'
@@ -0,0 +1,94 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/cmd_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/cmd'
13
+
14
+ ouc = Obfusk::Util::Cmd
15
+
16
+ describe 'obfusk/util/cmd' do
17
+
18
+ context 'killsig' do # {{{1
19
+ it 'SIGINT' do
20
+ ouc.killsig('SIGINT foo bar').should == \
21
+ { command: 'foo bar', signal: 'SIGINT' }
22
+ end
23
+ it 'default' do
24
+ ouc.killsig('foo bar').should == \
25
+ { command: 'foo bar', signal: 'SIGTERM' }
26
+ end
27
+ it 'set default' do
28
+ ouc.killsig('foo bar', 'SIGINT').should == \
29
+ { command: 'foo bar', signal: 'SIGINT' }
30
+ end
31
+ it 'no match' do
32
+ ouc.killsig('SIGx foo bar').should == \
33
+ { command: 'SIGx foo bar', signal: 'SIGTERM' }
34
+ end
35
+ end # }}}1
36
+
37
+ context 'shell' do # {{{1
38
+ it 'sh' do
39
+ ouc.shell('SHELL=sh foo bar').should == \
40
+ { command: 'foo bar', shell: 'sh' }
41
+ end
42
+ it 'default' do
43
+ ouc.shell('SHELL echo "$FOO: $BAR"').should == \
44
+ { command: 'echo "$FOO: $BAR"', shell: 'bash' }
45
+ end
46
+ it 'set default' do
47
+ ouc.shell('SHELL foo bar', 'mysh').should == \
48
+ { command: 'foo bar', shell: 'mysh' }
49
+ end
50
+ it 'no match' do
51
+ ouc.shell('SHELLx foo bar').should == \
52
+ { command: 'SHELLx foo bar', shell: nil }
53
+ end
54
+ end # }}}1
55
+
56
+ context 'nohup' do # {{{1
57
+ it 'nohup' do
58
+ ouc.nohup(*%w{ foo bar baz }).should == \
59
+ %w{ nohup foo bar baz }
60
+ end
61
+ end # }}}1
62
+
63
+ context 'set_vars' do # {{{1
64
+ it 'replace' do
65
+ ouc.set_vars('echo ${FOO} ... ${BAR} ...',
66
+ 'FOO' => 'foo', 'BAR' => 'bar').should == \
67
+ 'echo foo ... bar ...'
68
+ end
69
+ it 'multiple' do
70
+ ouc.set_vars('echo ${FOO} ... ${FOO} ...',
71
+ 'FOO' => 'foo').should == \
72
+ 'echo foo ... foo ...'
73
+ end
74
+ it 'missing' do
75
+ ouc.set_vars('echo ${FOO} ... ${BAR} ...',
76
+ 'BAR' => 'bar').should == \
77
+ 'echo ... bar ...'
78
+ end
79
+ end # }}}1
80
+
81
+ context 'env_to_a' do # {{{1
82
+ it 'simple' do
83
+ ouc.env_to_a('FOO' => 'bar', 'PORT' => 1234).sort.should == \
84
+ ['PORT=1234', 'FOO="bar"'].sort
85
+ end
86
+ it 'nils' do
87
+ ouc.env_to_a('FOO' => 'bar', 'PORT' => nil).should == \
88
+ ['FOO="bar"']
89
+ end
90
+ end # }}}1
91
+
92
+ end
93
+
94
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,77 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/data_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/data'
13
+
14
+ module Obfusk::Util::Data__Spec
15
+ S = Struct.new :x, :y
16
+ end
17
+
18
+ ou = Obfusk::Util
19
+ ds = ou::Data__Spec
20
+
21
+ describe 'obfusk/util/data' do
22
+
23
+ context 'assoc' do # {{{1
24
+ it 'assoc' do
25
+ x = { x: { y: 0 }, z: [1,2,3] }
26
+ ou.assoc(x, [:x,:y] => 1, [:z,1] => 99)
27
+ x[:x][:y].should == 1
28
+ x[:z].should == [1,99,3]
29
+ end
30
+ end # }}}1
31
+
32
+ context 'deepdup' do # {{{1
33
+ it 'equal' do
34
+ x = { x: { y: 0 }, z: [1,2,3], s: ds::S.new(1,2) }
35
+ y = Obfusk::Util.deepdup x
36
+ x.should == y
37
+ end
38
+ it 'different objects' do
39
+ x = { x: { y: 0 }, z: [1,2,3], s: ds::S.new(1,2) }
40
+ y = Obfusk::Util.deepdup x
41
+ y[:x][:y] = 1 ; x[:x][:y].should == 0
42
+ y[:z][1] = 99; x[:z][1].should == 2
43
+ y[:s].y = 37; x[:s].y.should == 2
44
+ end
45
+ end # }}}1
46
+
47
+ context 'empty_as_nil' do # {{{1
48
+ it 'nil' do
49
+ ou.empty_as_nil(nil).should == nil
50
+ end
51
+ it 'empty string' do
52
+ ou.empty_as_nil('').should == nil
53
+ end
54
+ it 'empty array' do
55
+ ou.empty_as_nil([]).should == nil
56
+ end
57
+ it 'not empty or nil' do
58
+ ou.empty_as_nil('foo').should == 'foo'
59
+ end
60
+ it 'real world example' do
61
+ env = { 'PORT' => '' }
62
+ (ou.empty_as_nil(env['PORT']) || 1234).should == 1234
63
+ end
64
+ end # }}}1
65
+
66
+ context 'h_to_struct' do # {{{1
67
+ it 'h_to_struct' do
68
+ s = ou.h_to_struct({x:1,y:2})
69
+ s.members.sort.should == [:x,:y].sort
70
+ s.x.should == 1
71
+ s.y.should == 2
72
+ end
73
+ end # }}}1
74
+
75
+ end
76
+
77
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,73 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/die_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/die'
13
+ require 'obfusk/util/message'
14
+ require 'obfusk/util/spec'
15
+
16
+ ou = Obfusk::Util
17
+
18
+ describe 'obfusk/util/die' do
19
+
20
+ context 'die!' do # {{{1
21
+ it 'exit + messages' do
22
+ ou.capture_stderr do
23
+ expect { ou.die!(*%w{ foo bar baz }) } \
24
+ .to raise_error(SystemExit)
25
+ end .should == "Error: foo\nError: bar\nError: baz\n"
26
+ end
27
+ it 'default value' do
28
+ begin ou.capture_stderr { ou.die!('foo') }
29
+ rescue SystemExit => e; e; end .status .should == 1
30
+ end
31
+ it 'default value' do
32
+ begin ou.capture_stderr { ou.die!('foo', status: 42) }
33
+ rescue SystemExit => e; e; end .status .should == 42
34
+ end
35
+ end # }}}1
36
+
37
+ context 'udie!' do # {{{1
38
+ usage = 'prog [<opt(s)>]'
39
+ it 'exit + messages' do
40
+ ou.capture_stderr do
41
+ expect { ou.udie!(usage, *%w{ foo bar }) } \
42
+ .to raise_error(SystemExit)
43
+ end .should == "Error: foo\nError: bar\nUsage: #{usage}\n"
44
+ end
45
+ it 'default value' do
46
+ begin ou.capture_stderr { ou.udie!(usage) }
47
+ rescue SystemExit => e; e; end .status .should == 1
48
+ end
49
+ it 'default value' do
50
+ begin ou.capture_stderr { ou.udie!(usage, status: 42) }
51
+ rescue SystemExit => e; e; end .status .should == 42
52
+ end
53
+ end # }}}1
54
+
55
+ context 'odie!' do # {{{1
56
+ it 'exit + message' do
57
+ ou.capture_stderr do
58
+ expect { ou.odie!('foo!') } .to raise_error(SystemExit)
59
+ end .should == "Error: foo!\n"
60
+ end
61
+ it 'default value' do
62
+ begin ou.capture_stderr { ou.odie!('foo!') }
63
+ rescue SystemExit => e; e; end .status .should == 1
64
+ end
65
+ it 'default value' do
66
+ begin ou.capture_stderr { ou.odie!('foo!', status: 42) }
67
+ rescue SystemExit => e; e; end .status .should == 42
68
+ end
69
+ end # }}}1
70
+
71
+ end
72
+
73
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,95 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/fs_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/fs'
13
+ require 'obfusk/util/message'
14
+
15
+ require 'fileutils'
16
+ require 'tmpdir'
17
+
18
+ ou = Obfusk::Util
19
+ fs = Obfusk::Util::FS
20
+
21
+ describe 'obfusk/util/fs' do
22
+
23
+ context 'append' do # {{{1
24
+ it 'new file' do
25
+ Dir.mktmpdir do |dir|
26
+ fs.append "#{dir}/testfile", %w{ foo bar baz }
27
+ File.read("#{dir}/testfile").should == "foo\nbar\nbaz\n"
28
+ end
29
+ end
30
+ it 'existing file' do
31
+ Dir.mktmpdir do |dir|
32
+ File.write "#{dir}/testfile", "some data\n"
33
+ fs.append "#{dir}/testfile", %w{ foo bar baz }
34
+ File.read("#{dir}/testfile").should == \
35
+ "some data\nfoo\nbar\nbaz\n"
36
+ end
37
+ end
38
+ end # }}}1
39
+
40
+ context 'exists?' do # {{{1
41
+ it 'existing file' do
42
+ Dir.mktmpdir do |dir|
43
+ FileUtils.touch "#{dir}/testfile"
44
+ fs.exists?("#{dir}/testfile").should be_true
45
+ end
46
+ end
47
+ it 'existing link' do
48
+ Dir.mktmpdir do |dir|
49
+ FileUtils.touch "#{dir}/target"
50
+ File.symlink "#{dir}/target", "#{dir}/symlink"
51
+ fs.exists?("#{dir}/symlink").should be_true
52
+ end
53
+ end
54
+ it 'existing dir' do
55
+ Dir.mktmpdir do |dir|
56
+ fs.exists?(dir).should be_true
57
+ end
58
+ end
59
+ it 'non-existing file' do
60
+ Dir.mktmpdir do |dir|
61
+ fs.exists?("#{dir}/testfile").should be_false
62
+ end
63
+ end
64
+ it 'non-existing link' do
65
+ Dir.mktmpdir do |dir|
66
+ fs.exists?("#{dir}/symlink").should be_false
67
+ end
68
+ end
69
+ it 'non-existing dir' do
70
+ Dir.mktmpdir do |dir|
71
+ fs.exists?("#{dir}/nonexistent").should be_false
72
+ end
73
+ end
74
+ it 'broken link' do
75
+ Dir.mktmpdir do |dir|
76
+ File.symlink "#{dir}/target", "#{dir}/symlink"
77
+ fs.exists?("#{dir}/symlink").should be_true
78
+ end
79
+ end
80
+ end # }}}1
81
+
82
+ context 'omkdir_p' do # {{{1
83
+ it 'exists + message' do
84
+ Dir.mktmpdir do |dir|
85
+ dirs = %w{ foo bar baz }.map { |x| "#{dir}/#{x}" }
86
+ ou.capture_stdout { fs.omkdir_p(*dirs) } \
87
+ .should == "==> mkdir -p #{dirs*' '}\n"
88
+ dirs.each { |d| Dir.exists?(d).should be_true }
89
+ end
90
+ end
91
+ end # }}}1
92
+
93
+ end
94
+
95
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,67 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/message_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/message'
13
+ require 'obfusk/util/spec'
14
+
15
+ ou = Obfusk::Util
16
+
17
+ describe 'obfusk/util/message' do
18
+
19
+ context 'ohai' do # {{{1
20
+ it 'no colours' do
21
+ ou.capture_stdout { ou.ohai 'hi there!' } .should == \
22
+ "==> hi there!\n"
23
+ end
24
+ it 'colours' do
25
+ ou.capture_stdout(:tty) { ou.ohai 'hi there!' } \
26
+ .should == "\e[1;34m==> \e[1;37mhi there!\e[0m\n"
27
+ end
28
+ end # }}}1
29
+
30
+ context 'onow' do # {{{1
31
+ it 'no colours' do
32
+ ou.capture_stdout { ou.onow 'Do', *%w{ foo bar } } .should == \
33
+ "==> Do: foo, bar\n"
34
+ end
35
+ it 'colours' do
36
+ ou.capture_stdout(:tty) { ou.onow 'Do', *%w{ foo bar } } \
37
+ .should == "\e[1;32m==> \e[1;37mDo\e[0m: " +
38
+ "\e[1;32mfoo\e[0m, \e[1;32mbar\e[0m\n"
39
+ end
40
+ end # }}}1
41
+
42
+ context 'onoe' do # {{{1
43
+ it 'no colours' do
44
+ ou.capture_stderr { ou.onoe 'oops!' } .should == \
45
+ "Error: oops!\n"
46
+ end
47
+ it 'colours' do
48
+ ou.capture_stderr(:tty) { ou.onoe 'oops!' } \
49
+ .should == "\e[1;31mError\e[0m: oops!\n"
50
+ end
51
+ it 'logger' do
52
+ log = []; l = ->(msg) { log << msg }
53
+ ou.capture_stderr { ou.onoe 'oops!', log: l }
54
+ log.should == ['Error: oops!']
55
+ end
56
+ end # }}}1
57
+
58
+ context 'opoo' do # {{{1
59
+ it 'label' do
60
+ ou.capture_stderr { ou.opoo 'oops!' } .should == \
61
+ "Warning: oops!\n"
62
+ end
63
+ end # }}}1
64
+
65
+ end
66
+
67
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1 @@
1
+ # nothing to test yet
@@ -0,0 +1,56 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/module_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/module'
13
+
14
+ require 'fileutils'
15
+ require 'tmpdir'
16
+
17
+ module Obfusk::Util::Module__Spec
18
+ module Foo; end; module Bar; end
19
+ end
20
+
21
+ ou = Obfusk::Util
22
+ ms = ou::Module__Spec
23
+
24
+ describe 'obfusk/util/module' do
25
+
26
+ context 'require_all' do # {{{1
27
+ it 'require_all' do
28
+ Dir.mktmpdir do |dir|
29
+ d = "#{dir}/obfusk/util/spec__"
30
+ FileUtils.mkdir_p d
31
+ %w{ foo bar }.each do |x|
32
+ File.write "#{d}/#{x}.rb",
33
+ "module #{ms}::#{x.capitalize}; VALUE = '#{x}'; end"
34
+ end
35
+ $:.unshift dir
36
+ begin
37
+ ou.require_all 'obfusk/util/spec__'
38
+ ms::Foo::VALUE.should == 'foo'
39
+ ms::Bar::VALUE.should == 'bar'
40
+ ensure
41
+ $:.shift
42
+ end
43
+ end
44
+ end
45
+ end # }}}1
46
+
47
+ context 'submodules' do # {{{1
48
+ it 'submodules' do
49
+ ou.submodules(ms).should == \
50
+ { 'foo' => ms::Foo, 'bar' => ms::Bar }
51
+ end
52
+ end # }}}1
53
+
54
+ end
55
+
56
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,48 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/opt_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/opt'
13
+
14
+ op = Obfusk::Util::Opt
15
+
16
+ describe 'obfusk/util/opt' do
17
+
18
+ context 'Parser' do # {{{1
19
+ it 'parse_r' do
20
+ x = nil
21
+ p = op::Parser.new do |o|
22
+ o.on('--help', 'Show help') { x = 'HELP' }
23
+ end
24
+ args1 = %w{ foo --help bar }; args2 = p.parse_r args1
25
+ x.should == 'HELP'
26
+ args1.should == %w{ foo --help bar }
27
+ args2.should == %w{ foo bar }
28
+ end
29
+ it 'no officious' do
30
+ expect { op::Parser.new.parse_r %w{ --help } } .to \
31
+ raise_error(OptionParser::InvalidOption, /--help/)
32
+ end
33
+ end # }}}1
34
+
35
+ context 'parse' do # {{{1
36
+ it 'parse' do
37
+ p = op::Parser.new do |o|
38
+ o.on('--help', 'Show help') {}
39
+ end
40
+ args1 = %w{ foo --help bar }; args2 = op.parse p, args1
41
+ args1.should == %w{ foo --help bar }
42
+ args2.should == %w{ foo bar }
43
+ end
44
+ end # }}}1
45
+
46
+ end
47
+
48
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,41 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/os_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/os'
13
+
14
+ os = Obfusk::Util::OS
15
+
16
+ describe 'obfusk/util/os' do
17
+
18
+ context 'home' do # {{{1
19
+ it 'my home' do
20
+ os.home.should == ENV['HOME'] # ????
21
+ end
22
+ it 'root' do
23
+ os.home('root').should == '/root' # ????
24
+ end
25
+ end # }}}1
26
+
27
+ context 'user' do # {{{1
28
+ it 'user' do
29
+ os.user.should == ENV['USER'] # ????
30
+ end
31
+ end # }}}1
32
+
33
+ context 'now' do # {{{1
34
+ it 'now' do
35
+ os.now.should match /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/
36
+ end
37
+ end # }}}1
38
+
39
+ end
40
+
41
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,60 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/process_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/process'
13
+
14
+ pr = Obfusk::Util::Process
15
+
16
+ describe 'obfusk/util/process' do
17
+
18
+ context 'age' do # {{{1
19
+ it 'age' do
20
+ pid = spawn 'sleep 100'
21
+ begin
22
+ pr.age(pid).should match /^00:0\d$/ # ????
23
+ ensure
24
+ Process.kill 'TERM', pid; Process.wait pid
25
+ end
26
+ end
27
+ end # }}}1
28
+
29
+ context 'alive' do # {{{1
30
+ it 'alive' do
31
+ pid = spawn 'sleep 100'
32
+ begin
33
+ pr.alive?(pid).should == true
34
+ ensure
35
+ Process.kill 'TERM', pid; Process.wait pid
36
+ end
37
+ end
38
+ it 'not alive' do
39
+ pid = spawn 'sleep 100'
40
+ Process.kill 'TERM', pid; Process.wait pid
41
+ pr.alive?(pid).should == false
42
+ end
43
+ it 'not mine' do
44
+ pr.alive?(1).should == :not_mine # init should be alive # ????
45
+ end
46
+ end # }}}1
47
+
48
+ context 'ispid!' do # {{{1
49
+ it 'pid' do
50
+ pr.ispid!(12345).should be_true
51
+ end
52
+ it 'not a pid' do
53
+ expect { pr.ispid!('12345') } .to \
54
+ raise_error(ArgumentError, 'invalid PID')
55
+ end
56
+ end # }}}1
57
+
58
+ end
59
+
60
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,134 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/run_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/message'
13
+ require 'obfusk/util/run'
14
+
15
+ ou = Obfusk::Util
16
+
17
+ describe 'obfusk/util/run' do
18
+
19
+ context 'exec' do # {{{1
20
+ it 'succeed' do
21
+ r, w = IO.pipe
22
+ pid = fork do
23
+ r.close; ou.exec 'bash', '-c', 'echo $FOO',
24
+ env: { 'FOO' => 'foo' }, out: w
25
+ end
26
+ w.close; Process.wait pid; x = r.read; r.close
27
+ x.should == "foo\n"
28
+ $?.exitstatus.should == 0
29
+ end
30
+ it 'fail (no shell)' do
31
+ expect { ou.exec 'echo THIS IS NOT A COMMAND' } .to \
32
+ raise_error(ou::RunError, /No such file or directory/) # ????
33
+ end
34
+ end # }}}1
35
+
36
+ context 'spawn' do # {{{1
37
+ it 'succeed' do
38
+ r, w = IO.pipe
39
+ pid = ou.spawn 'bash', '-c', 'echo $FOO; pwd',
40
+ env: { 'FOO' => 'foo' }, out: w, chdir: '/'
41
+ w.close; Process.wait pid; x = r.read; r.close
42
+ x.should == "foo\n/\n"
43
+ $?.exitstatus.should == 0
44
+ end
45
+ it 'fail (no shell)' do
46
+ expect { ou.spawn 'echo THIS IS NOT A COMMAND' } .to \
47
+ raise_error(ou::RunError, /No such file or directory/) # ????
48
+ end
49
+ end # }}}1
50
+
51
+ context 'spawn_w' do # {{{1
52
+ it 'succeed' do
53
+ r, w = IO.pipe
54
+ res = ou.spawn_w 'bash', '-c', 'echo $FOO; pwd',
55
+ env: { 'FOO' => 'foo' }, out: w, chdir: '/'
56
+ w.close; x = r.read; r.close
57
+ x.should == "foo\n/\n"
58
+ res.exitstatus.should == 0
59
+ end
60
+ it 'fail (no shell)' do
61
+ expect { ou.spawn_w 'echo THIS IS NOT A COMMAND' } .to \
62
+ raise_error(ou::RunError, /No such file or directory/) # ????
63
+ end
64
+ end # }}}1
65
+
66
+ context 'system' do # {{{1
67
+ it 'succeed' do
68
+ r, w = IO.pipe
69
+ res = ou.system 'bash', '-c', 'echo $FOO; pwd',
70
+ env: { 'FOO' => 'foo' }, out: w, chdir: '/'
71
+ w.close; x = r.read; r.close
72
+ x.should == "foo\n/\n"
73
+ res.should == true
74
+ end
75
+ it 'fail (no shell)' do
76
+ expect { ou.system 'echo THIS IS NOT A COMMAND' } .to \
77
+ raise_error(ou::RunError, /failed to run command/)
78
+ end
79
+ end # }}}1
80
+
81
+ context 'popen3' do # {{{1
82
+ it 'succeed' do
83
+ ou.popen3 'bash', '-c', 'echo $FOO >&2; pwd',
84
+ env: { 'FOO' => 'foo' }, chdir: '/' do |i,o,e,t|
85
+ i.close
86
+ t.value.exitstatus.should == 0
87
+ o.read.should == "/\n"
88
+ e.read.should == "foo\n"
89
+ end
90
+ end
91
+ it 'fail (no shell)' do
92
+ expect { ou.popen3 'echo THIS IS NOT A COMMAND' } .to \
93
+ raise_error(ou::RunError, /No such file or directory/) # ????
94
+ end
95
+ end # }}}1
96
+
97
+ context 'ospawn' do # {{{1
98
+ it 'colour' do
99
+ r, w = IO.pipe
100
+ ou.capture_stdout(:tty) do
101
+ pid = ou.ospawn *%w{ echo FOO }, out: w
102
+ w.close; Process.wait pid; x = r.read; r.close
103
+ x.should == "FOO\n"
104
+ $?.exitstatus.should == 0
105
+ end .should == "\e[1;34m==> \e[1;37mecho FOO\e[0m\n"
106
+ end
107
+ end # }}}1
108
+
109
+ context 'ospawn_w' do # {{{1
110
+ it 'colour' do
111
+ r, w = IO.pipe
112
+ ou.capture_stdout(:tty) do
113
+ res = ou.ospawn_w *%w{ echo FOO }, out: w
114
+ w.close; x = r.read; r.close
115
+ x.should == "FOO\n"
116
+ res.exitstatus.should == 0
117
+ end .should == "\e[1;34m==> \e[1;37mecho FOO\e[0m\n"
118
+ end
119
+ end # }}}1
120
+
121
+ context 'chk_exit' do # {{{1
122
+ it 'zero' do
123
+ expect { ou.chk_exit(%w{ true }) { |a| ou.spawn_w *a } } \
124
+ .to_not raise_error
125
+ end
126
+ it 'non-zero' do
127
+ expect { ou.chk_exit(%w{ false }) { |a| ou.spawn_w *a } } \
128
+ .to raise_error(ou::RunError, /command returned non-zero/)
129
+ end
130
+ end # }}}1
131
+
132
+ end
133
+
134
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,66 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/spec_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/spec'
13
+
14
+ ou = Obfusk::Util
15
+
16
+ describe 'obfusk/util/spec' do
17
+
18
+ context 'provide_stdin' do # {{{1
19
+ it 'stdin + reset' do
20
+ old = $stdin
21
+ ou.provide_stdin("foo\nbar\nbaz\n") do
22
+ $stdin.readlines.map(&:chomp)
23
+ end .should == %w{ foo bar baz }
24
+ $stdin.should == old
25
+ end
26
+ it 'reset on error' do
27
+ err = Class.new RuntimeError; old = $stdin
28
+ begin ou.provide_stdin('') { raise err, 'OOPS' }
29
+ rescue err; end
30
+ $stdin.should == old
31
+ end
32
+ end # }}}1
33
+
34
+ context 'capture_stdout' do # {{{1
35
+ it 'stdout + reset' do
36
+ old = $stdout
37
+ ou.capture_stdout { puts %w{ foo bar baz } } \
38
+ .should == "foo\nbar\nbaz\n"
39
+ $stdout.should == old
40
+ end
41
+ it 'reset on error' do
42
+ err = Class.new RuntimeError; old = $stdout
43
+ begin ou.capture_stdout { raise err, 'OOPS' }
44
+ rescue err; end
45
+ $stdout.should == old
46
+ end
47
+ end # }}}1
48
+
49
+ context 'capture_stderr' do # {{{1
50
+ it 'stderr + reset' do
51
+ old = $stderr
52
+ ou.capture_stderr { $stderr.puts %w{ foo bar baz } } \
53
+ .should == "foo\nbar\nbaz\n"
54
+ $stderr.should == old
55
+ end
56
+ it 'reset on error' do
57
+ err = Class.new RuntimeError; old = $stderr
58
+ begin ou.capture_stderr { raise err, 'OOPS' }
59
+ rescue err; end
60
+ $stderr.should == old
61
+ end
62
+ end # }}}1
63
+
64
+ end
65
+
66
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,71 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/struct_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/struct'
13
+
14
+ ou = Obfusk::Util
15
+
16
+ struct = ->() {
17
+ ou.struct(*%w{ f1 f2 f3 }) do
18
+ def some_method; f1 + f2; end
19
+ end
20
+ }
21
+
22
+ describe 'obfusk/util/struct' do
23
+
24
+ context 'struct' do # {{{1
25
+ it 'new' do
26
+ s = struct[]; foo = s.new f1: 37, f2: 5
27
+ foo.some_method.should == 42
28
+ foo.f1.should == 37
29
+ foo.f2.should == 5
30
+ foo.f3.should == nil
31
+ end
32
+ it 'build' do
33
+ s = struct[]; bar = s.build(f1: 99) { |x| x.f2 = 1 }
34
+ bar.some_method.should == 100
35
+ bar.f1.should == 99
36
+ bar.f2.should == 1
37
+ bar.f3.should == nil
38
+ end
39
+ it 'no other fields to get' do
40
+ s = struct[]; bar = s.new
41
+ expect { bar.oops } .to \
42
+ raise_error(NoMethodError, /undefined method.*`oops'/)
43
+ end
44
+ it 'no other fields to set' do
45
+ s = struct[]; bar = s.new
46
+ expect { bar.oops = 99 } .to \
47
+ raise_error(NoMethodError, /undefined method.*`oops='/)
48
+ end
49
+ it 'frozen' do
50
+ s = struct[]; bar = s.build { |x| x.f2 = 1 }
51
+ expect { bar.f1 = 99 } .to \
52
+ raise_error(RuntimeError, /can't modify frozen/)
53
+ end
54
+ it 'check!' do
55
+ s = struct[]; bar = s.build { |x| x.f2 = 1 }
56
+ expect { bar.check! } .to \
57
+ raise_error(ou::BetterStruct::IncompleteError, /empty field/)
58
+ end
59
+ it 'to_h' do
60
+ s = struct[]; bar = s.new(f1: 'hi!')
61
+ bar.to_h.should == { f1: 'hi!', f2: nil, f3: nil }
62
+ end
63
+ it 'to_str_h' do
64
+ s = struct[]; bar = s.new(f1: 'hi!')
65
+ bar.to_str_h.should == { 'f1'=>'hi!', 'f2'=>nil, 'f3'=>nil }
66
+ end
67
+ end # }}}1
68
+
69
+ end
70
+
71
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,104 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/term_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/term'
13
+
14
+ ou = Obfusk::Util
15
+ te = Obfusk::Util::Term
16
+
17
+ describe 'obfusk/util/term' do
18
+
19
+ context 'colour' do # {{{1
20
+ it 'no tty' do
21
+ ou.capture_stdout { te.colour(:red) } .should == ''
22
+ end
23
+ it 'no such colour' do
24
+ expect { te.colour(:octarine) } .to \
25
+ raise_error(ArgumentError, /No such colour/)
26
+ end
27
+ it 'red, blue, green' do
28
+ ou.capture_stdout(:tty) do
29
+ te.colour(:red) .should == "\e[0;31m"
30
+ te.colour(:blue) .should == "\e[0;34m"
31
+ te.colour(:green) .should == "\e[0;32m"
32
+ end
33
+ end
34
+ it 'red, blue, green (stderr)' do
35
+ ou.capture_stderr(:tty) do
36
+ te.colour_e(:red) .should == "\e[0;31m"
37
+ te.colour_e(:blue) .should == "\e[0;34m"
38
+ te.colour_e(:green) .should == "\e[0;32m"
39
+ end
40
+ end
41
+ end # }}}1
42
+
43
+ context 'columns' do # {{{1
44
+ it 'columns' do
45
+ old = ENV['COLUMNS']; ENV['COLUMNS'] = '17'
46
+ begin te.columns.should == 17
47
+ ensure ENV['COLUMNS'] = old end
48
+ end
49
+ end # }}}1
50
+
51
+ context 'lines' do # {{{1
52
+ it 'lines' do
53
+ old = ENV['LINES']; ENV['LINES'] = '71'
54
+ begin te.lines.should == 71
55
+ ensure ENV['LINES'] = old end
56
+ end
57
+ end # }}}1
58
+
59
+ context 'tty?' do # {{{1
60
+ it 'stdout tty' do
61
+ ou.capture_stdout(:tty) { te.tty?.should be_true }
62
+ end
63
+ it 'stderr tty' do
64
+ ou.capture_stderr(:tty) { te.tty?(:err).should be_true }
65
+ end
66
+ it 'stdout no tty' do
67
+ ou.capture_stdout { te.tty?.should be_false }
68
+ end
69
+ it 'stderr no tty' do
70
+ ou.capture_stderr { te.tty?(:err).should be_false }
71
+ end
72
+ end # }}}1
73
+
74
+ context 'prompt' do # {{{1
75
+ it 'normal' do
76
+ ou.provide_stdin("foo!\n") do
77
+ ou.capture_stdout do
78
+ name = te.prompt 'name? '
79
+ name.should == 'foo!'
80
+ end .should == 'name? '
81
+ end
82
+ end
83
+ it 'no newline' do
84
+ ou.provide_stdin('foo!') do
85
+ ou.capture_stdout do
86
+ name = te.prompt 'name? '
87
+ name.should == 'foo!'
88
+ end .should == 'name? '
89
+ end
90
+ end
91
+ it 'hide' do
92
+ ou.provide_stdin('foo!') do
93
+ def $stdin.noecho(&b) b[self] end # mock!
94
+ ou.capture_stdout do
95
+ name = te.prompt 'name? ', :hide
96
+ name.should == 'foo!'
97
+ end .should == "name? \n"
98
+ end
99
+ end
100
+ end # }}}1
101
+
102
+ end
103
+
104
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,65 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : obfusk/util/valid_spec.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-07-24
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'obfusk/util/valid'
13
+
14
+ va = Obfusk::Util::Valid
15
+
16
+ describe 'obfusk/util/valid' do
17
+
18
+ context 'args' do # {{{1
19
+ it 'max = min' do
20
+ expect { va.args 'foo', %w{ a b c }, 3 } .to_not raise_error
21
+ end
22
+ it 'max + min' do
23
+ expect { va.args 'foo', %w{ a b c }, 1, 4 } .to_not raise_error
24
+ expect { va.args 'foo', %w{ a b c }, 2, 3 } .to_not raise_error
25
+ expect { va.args 'foo', %w{ a b c }, 3, 4 } .to_not raise_error
26
+ expect { va.args 'foo', %w{ a b c }, 3, 3 } .to_not raise_error
27
+ end
28
+ it 'no max' do
29
+ expect { va.args 'foo', %w{ a b }, 1, nil } .to_not raise_error
30
+ expect { va.args 'foo', %w{ a b }, 2, nil } .to_not raise_error
31
+ end
32
+ it 'max = min (invalid)' do
33
+ expect { va.args 'foo', %w{ a b c }, 2 } .to \
34
+ raise_error(va::ArgumentError)
35
+ expect { va.args 'foo', %w{ a b c }, 4 } .to \
36
+ raise_error(va::ArgumentError)
37
+ end
38
+ it 'max + min (invalid)' do
39
+ expect { va.args 'foo', %w{ a b c }, 1, 2 } .to \
40
+ raise_error(va::ArgumentError)
41
+ expect { va.args 'foo', %w{ a b c }, 4, 5 } .to \
42
+ raise_error(va::ArgumentError)
43
+ end
44
+ it 'no max (invalid)' do
45
+ expect { va.args 'foo', %w{ a b }, 3, nil } .to \
46
+ raise_error(va::ArgumentError)
47
+ end
48
+ end # }}}1
49
+
50
+ context 'validate!' do # {{{1
51
+ it 'nil' do
52
+ expect { va.validate! nil, /.*/, '...' } .to_not raise_error
53
+ end
54
+ it 'valid' do
55
+ expect { va.validate! 'foo', /fo+/, '...' } .to_not raise_error
56
+ end
57
+ it 'invalid' do
58
+ expect { va.validate! 'f', /fo+/, 'oops' } .to \
59
+ raise_error(va::ValidationError, 'invalid oops')
60
+ end
61
+ end # }}}1
62
+
63
+ end
64
+
65
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obfusk-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.SNAPSHOT.20130724235009
4
+ version: 0.0.1.SNAPSHOT.20130725000153
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-07-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &7444640 !ruby/object:Gem::Requirement
16
+ requirement: &14769780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *7444640
24
+ version_requirements: *14769780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &7443620 !ruby/object:Gem::Requirement
27
+ requirement: &14764420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *7443620
35
+ version_requirements: *14764420
36
36
  description: ! 'miscellaneous utility library for ruby
37
37
 
38
38
 
@@ -45,7 +45,9 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - .yardopts
48
49
  - README.md
50
+ - Rakefile
49
51
  - obfusk-util.gemspec
50
52
  - lib/obfusk/util/message.rb
51
53
  - lib/obfusk/util/struct.rb
@@ -64,6 +66,21 @@ files:
64
66
  - lib/obfusk/util/die.rb
65
67
  - lib/obfusk/util/module.rb
66
68
  - lib/obfusk/util/cmd.rb
69
+ - spec/obfusk/util/misc_spec.rb
70
+ - spec/obfusk/util/fs_spec.rb
71
+ - spec/obfusk/util/opt_spec.rb
72
+ - spec/obfusk/util/message_spec.rb
73
+ - spec/obfusk/util/os_spec.rb
74
+ - spec/obfusk/util/module_spec.rb
75
+ - spec/obfusk/util/data_spec.rb
76
+ - spec/obfusk/util/struct_spec.rb
77
+ - spec/obfusk/util/valid_spec.rb
78
+ - spec/obfusk/util/cmd_spec.rb
79
+ - spec/obfusk/util/die_spec.rb
80
+ - spec/obfusk/util/term_spec.rb
81
+ - spec/obfusk/util/process_spec.rb
82
+ - spec/obfusk/util/spec_spec.rb
83
+ - spec/obfusk/util/run_spec.rb
67
84
  homepage: https://github.com/obfusk/rb-obfusk-util
68
85
  licenses:
69
86
  - GPLv2