spex 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/README.md +66 -0
  2. data/Rakefile +1 -2
  3. data/VERSION +1 -1
  4. data/bin/spex +1 -1
  5. data/examples/chgrp.rb +5 -0
  6. data/examples/chmod.rb +5 -0
  7. data/examples/chown.rb +5 -0
  8. data/examples/puppet.rb +9 -0
  9. data/examples/touch.rb +5 -0
  10. data/examples/writing.rb +5 -0
  11. data/lib/spex.rb +4 -4
  12. data/lib/spex/assertion.rb +57 -13
  13. data/lib/spex/assertions/changed_group_assertion.rb +61 -0
  14. data/lib/spex/assertions/changed_mode_assertion.rb +48 -0
  15. data/lib/spex/assertions/changed_owner_assertion.rb +73 -0
  16. data/lib/spex/assertions/created_assertion.rb +26 -0
  17. data/lib/spex/assertions/file_assertion.rb +1 -10
  18. data/lib/spex/assertions/modified_assertion.rb +56 -0
  19. data/lib/spex/assertions/removed_assertion.rb +27 -0
  20. data/lib/spex/cli.rb +65 -36
  21. data/lib/spex/execution.rb +37 -0
  22. data/lib/spex/runner.rb +73 -51
  23. data/lib/spex/scenario.rb +22 -12
  24. data/lib/spex/script.rb +13 -18
  25. data/test/helper.rb +7 -0
  26. data/test/test_assertion.rb +58 -0
  27. data/test/test_script.rb +54 -0
  28. metadata +29 -42
  29. data/README.markdown +0 -116
  30. data/examples/chgrp/run.sh +0 -3
  31. data/examples/chgrp/scenarios.rb +0 -5
  32. data/examples/chmod/run.sh +0 -3
  33. data/examples/chmod/scenarios.rb +0 -5
  34. data/examples/chown/run.sh +0 -3
  35. data/examples/chown/scenarios.rb +0 -5
  36. data/examples/puppet/manifest.pp +0 -3
  37. data/examples/puppet/run.sh +0 -3
  38. data/examples/puppet/scenarios.rb +0 -5
  39. data/examples/touch/run.sh +0 -3
  40. data/examples/touch/scenarios.rb +0 -5
  41. data/lib/spex/assertions/chgrps_file_assertion.rb +0 -81
  42. data/lib/spex/assertions/chmods_file_assertion.rb +0 -56
  43. data/lib/spex/assertions/chowns_file_assertion.rb +0 -81
  44. data/lib/spex/assertions/creates_file_assertion.rb +0 -31
  45. data/lib/spex/assertions/removes_file_assertion.rb +0 -31
  46. data/lib/spex/script/builder.rb +0 -15
  47. data/test/test_stringup.rb +0 -7
data/lib/spex/script.rb CHANGED
@@ -1,34 +1,32 @@
1
1
  module Spex
2
2
  class Script
3
+ include Enumerable
3
4
 
4
5
  attr_accessor :command
5
6
 
6
7
  def self.evaluate_file(path)
7
- evaluate(File.read(path), path)
8
+ evaluate(File.read(path), path, 1)
8
9
  end
9
10
 
10
- def self.evaluate(text, path)
11
+ def self.evaluate(*args, &block)
11
12
  script = new
12
- Builder.new(script).instance_eval(text, path, 1)
13
+ builder = Builder.new(script, &block)
14
+ unless block_given?
15
+ builder.instance_eval(*args)
16
+ end
13
17
  script
14
18
  end
15
19
 
16
20
  def <<(scenario)
17
- scenarios[scenario.name.to_sym] = scenario
21
+ scenarios << scenario
18
22
  end
19
23
 
20
24
  def scenarios
21
- @scenarios ||= {}
25
+ @scenarios ||= []
22
26
  end
23
27
 
24
- def [](name)
25
- scenarios[name.to_sym]
26
- end
27
-
28
- def validate!
29
- unless @command
30
- abort "ERROR: The command was not set.\n\nExample:\n\n command 'cat %s'"
31
- end
28
+ def each(&block)
29
+ scenarios.each(&block)
32
30
  end
33
31
 
34
32
  class Builder
@@ -38,13 +36,10 @@ module Spex
38
36
  instance_eval(&block) if block_given?
39
37
  end
40
38
 
41
- def scenario(name, description = name.to_s, &block)
42
- scenario = ::Spex::Scenario.new(name, description, &block)
39
+ def scenario(name, &block)
40
+ scenario = ::Spex::Scenario.new(name, &block)
43
41
  @script << scenario
44
42
  end
45
- def command(line)
46
- @script.command = line
47
- end
48
43
 
49
44
  end
50
45
 
data/test/helper.rb CHANGED
@@ -7,4 +7,11 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
  require 'spex'
8
8
 
9
9
  class Test::Unit::TestCase
10
+
11
+ private
12
+
13
+ def script(&block)
14
+ @script = Spex::Script.evaluate(&block)
15
+ end
16
+
10
17
  end
@@ -0,0 +1,58 @@
1
+ require 'helper'
2
+
3
+ class TestAssertion < Test::Unit::TestCase
4
+ context "Assertion" do
5
+ setup do
6
+ @klass = Class.new(Spex::Assertion)
7
+ end
8
+
9
+ context "instances" do
10
+ should "raise an exception if instantiated with an unknown option" do
11
+ assert_raises Spex::Assertion::UnknownOptionError do
12
+ @klass.new('/tmp/foo', :unknown => 'option')
13
+ end
14
+ end
15
+ end
16
+
17
+ context "classes" do
18
+ context "after being registered" do
19
+ setup do
20
+ @klass.as :something, "Something being added"
21
+ end
22
+
23
+ should "be added to the list of assertions" do
24
+ assert_equal @klass, Spex::Assertion[:something]
25
+ end
26
+ end
27
+
28
+ context "setting an option" do
29
+ setup do
30
+ @klass.option :to, 'A desc'
31
+ end
32
+
33
+ should "add one to the mapping" do
34
+ assert_kind_of Hash, @klass.options
35
+ assert_equal 1, @klass.options.size
36
+ end
37
+
38
+ context "and the option" do
39
+ setup do
40
+ @option = @klass.options[:to]
41
+ end
42
+
43
+ should "be of the correct class" do
44
+ assert_kind_of Spex::Assertion::Option, @option
45
+ end
46
+
47
+ should "have a name" do
48
+ assert_equal :to, @option.name
49
+ end
50
+
51
+ should "have a description if given" do
52
+ assert_equal 'A desc', @option.description
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,54 @@
1
+ require 'helper'
2
+
3
+ class TestScript < Test::Unit::TestCase
4
+ context "evaluating" do
5
+ context "an empty scenario definition" do
6
+ setup do
7
+ script { scenario("name") { } }
8
+ end
9
+
10
+ should "create an empty scenario" do
11
+ assert_equal 1, @script.scenarios.size
12
+ assert_equal "name", @script.scenarios.first.name
13
+ end
14
+ end
15
+ context "a scenario definition with an executing definition" do
16
+ setup do
17
+ script { scenario("name") { executing('foo') { } } }
18
+ end
19
+
20
+ should "create an scenario" do
21
+ assert_equal 1, @script.scenarios.size
22
+ assert_equal "name", @script.scenarios.first.name
23
+ end
24
+
25
+ should "create an execution instance" do
26
+ assert_equal 1, @script.scenarios.first.executions.size
27
+ assert_equal 'foo', @script.scenarios.first.executions.first.command
28
+ end
29
+ end
30
+
31
+ context "a scenario and execution definition with an assertion" do
32
+ setup do
33
+ script do
34
+ scenario("name") do
35
+ executing('foo') do
36
+ assert '/tmp/foo', :created => true
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ should "create an assertion instance" do
43
+ execution = @script.scenarios.first.executions.first
44
+ assert_equal 1, execution.assertions.size
45
+ assertion = execution.assertions.first
46
+ assert_kind_of Spex::Assertion, assertion
47
+ assert_equal '/tmp/foo', assertion.target
48
+ assert assertion.active?
49
+ assert_equal({}, assertion.options)
50
+ end
51
+ end
52
+
53
+ end
54
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
7
+ - 5
8
8
  - 0
9
- version: 0.4.0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bruce Williams
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-08 00:00:00 -07:00
17
+ date: 2010-04-13 00:00:00 -07:00
18
18
  default_executable: spex
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: thor
21
+ name: colored
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
@@ -29,18 +29,6 @@ dependencies:
29
29
  version: "0"
30
30
  type: :runtime
31
31
  version_requirements: *id001
32
- - !ruby/object:Gem::Dependency
33
- name: shoulda
34
- prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- segments:
40
- - 0
41
- version: "0"
42
- type: :runtime
43
- version_requirements: *id002
44
32
  description: An easy-to-use test harness that runs assertions before and after and executable is run
45
33
  email: bruce@codefluency.com
46
34
  executables:
@@ -49,41 +37,38 @@ extensions: []
49
37
 
50
38
  extra_rdoc_files:
51
39
  - LICENSE
52
- - README.markdown
40
+ - README.md
53
41
  files:
54
42
  - .document
55
43
  - .gitignore
56
44
  - LICENSE
57
- - README.markdown
45
+ - README.md
58
46
  - Rakefile
59
47
  - VERSION
60
48
  - bin/spex
61
- - examples/chgrp/run.sh
62
- - examples/chgrp/scenarios.rb
63
- - examples/chmod/run.sh
64
- - examples/chmod/scenarios.rb
65
- - examples/chown/run.sh
66
- - examples/chown/scenarios.rb
67
- - examples/puppet/manifest.pp
68
- - examples/puppet/run.sh
69
- - examples/puppet/scenarios.rb
70
- - examples/touch/run.sh
71
- - examples/touch/scenarios.rb
49
+ - examples/chgrp.rb
50
+ - examples/chmod.rb
51
+ - examples/chown.rb
52
+ - examples/puppet.rb
53
+ - examples/touch.rb
54
+ - examples/writing.rb
72
55
  - lib/spex.rb
73
56
  - lib/spex/assertion.rb
74
- - lib/spex/assertions/chgrps_file_assertion.rb
75
- - lib/spex/assertions/chmods_file_assertion.rb
76
- - lib/spex/assertions/chowns_file_assertion.rb
77
- - lib/spex/assertions/creates_file_assertion.rb
57
+ - lib/spex/assertions/changed_group_assertion.rb
58
+ - lib/spex/assertions/changed_mode_assertion.rb
59
+ - lib/spex/assertions/changed_owner_assertion.rb
60
+ - lib/spex/assertions/created_assertion.rb
78
61
  - lib/spex/assertions/file_assertion.rb
79
- - lib/spex/assertions/removes_file_assertion.rb
62
+ - lib/spex/assertions/modified_assertion.rb
63
+ - lib/spex/assertions/removed_assertion.rb
80
64
  - lib/spex/cli.rb
65
+ - lib/spex/execution.rb
81
66
  - lib/spex/runner.rb
82
67
  - lib/spex/scenario.rb
83
68
  - lib/spex/script.rb
84
- - lib/spex/script/builder.rb
85
69
  - test/helper.rb
86
- - test/test_stringup.rb
70
+ - test/test_assertion.rb
71
+ - test/test_script.rb
87
72
  has_rdoc: true
88
73
  homepage: http://github.com/bruce/spex
89
74
  licenses: []
@@ -116,9 +101,11 @@ specification_version: 3
116
101
  summary: A test harness for executables
117
102
  test_files:
118
103
  - test/helper.rb
119
- - test/test_stringup.rb
120
- - examples/chgrp/scenarios.rb
121
- - examples/chmod/scenarios.rb
122
- - examples/chown/scenarios.rb
123
- - examples/puppet/scenarios.rb
124
- - examples/touch/scenarios.rb
104
+ - test/test_assertion.rb
105
+ - test/test_script.rb
106
+ - examples/chgrp.rb
107
+ - examples/chmod.rb
108
+ - examples/chown.rb
109
+ - examples/puppet.rb
110
+ - examples/touch.rb
111
+ - examples/writing.rb
data/README.markdown DELETED
@@ -1,116 +0,0 @@
1
- Spex
2
- ========
3
-
4
- A quick and dirty test harness for testing assertions before and after
5
- an executable is run.
6
-
7
- Synopsis
8
- --------
9
-
10
- Spex is a simple language used to define scenarios that model
11
- the correct behavior of an executable.
12
-
13
- The description file consists of exactly one `command` line and any
14
- number of `scenario` definitions; for example, the following file can
15
- be used to verify running `touch /tmp/foo` will create a new file:
16
-
17
- command 'touch /tmp/foo'
18
-
19
- scenario :new, "Creates a file" do
20
- assert_creates_file '/tmp/foo'
21
- end
22
-
23
- If this was in `scenarios.rb`, you could run this with spex:
24
-
25
- $ spex execute scenarios.rb --scenario new
26
-
27
- If you had named the scenario `default`, the `--scenario` option
28
- wouldn't have been necessary, ie:
29
-
30
- scenario :default "Creates a file" do
31
- assert_creates_file '/tmp/foo'
32
- end
33
-
34
- $ spex execute scenarios.rb
35
-
36
- You'll notice that this should pass the first time and fail on
37
- subsequent invocations -- because the `assert_creates` fails in the
38
- event a file exists *before* the command is run.
39
-
40
- If you want to see what command and scenarios are defined in a file,
41
- use `spex info`, eg:
42
-
43
- $ spex info scenarios.rb
44
-
45
- Commands with arguments
46
- -----------------------
47
-
48
- Let's say you had an executable that reads in a configuration file and
49
- has some type of side-effect. You'd like to test running the
50
- executable against multiple configuration files checking a scenario,
51
- without having to edit the spex file every time, changing the path
52
- to the configuration file.
53
-
54
- Luckily the command can be provided in `sprintf` style. Assuming our
55
- executable is named `myexec` and you pass the configuration file to it
56
- via `-c`, the following would work:
57
-
58
- command 'myexec -c %s'
59
-
60
- Now, you just pass more options to `spex execute`:
61
-
62
- $ spex execute scenarios.rb /path/to/my/configuration.conf
63
-
64
- .. and it's just as if you ran:
65
-
66
- $ myexec -c /path/to/my/configuration.conf
67
-
68
- Usage help
69
- ----------
70
-
71
- See the commandline help documentation:
72
-
73
- $ spex
74
-
75
- For more information on specific commands, you'll want to use `help`,
76
- eg:
77
-
78
- $ spex help execute
79
-
80
- Examples
81
- --------
82
-
83
- See the `examples/` directory.
84
-
85
- Assertions
86
- ----------
87
-
88
- The list of assertions is very short at this point.
89
-
90
- To add an assertion, create a class that inherits from
91
- `Spex::Assertion` and implements all the neccessary methods. See
92
- `Spex::Assertion` and the currently defined assertions for
93
- examples.
94
-
95
- Note: If you put your assertions in `~/.spex/assertions/*.rb`,
96
- they'll automatically be loaded. If you create any interesting
97
- assertions, make sure you let me know!
98
-
99
- ### assert_creates_file
100
-
101
- Checks to see if a file was created.
102
-
103
- You can pass `:file => true` or `:directory => true` to ensure the
104
- file is a regular file or directory.
105
-
106
- ### assert_removes_file
107
-
108
- Checks to see if a file was removed.
109
-
110
- You can pass `:file => true` or `:directory => true` to ensure the
111
- file was a regular file or directory before being removed.
112
-
113
- Copyright
114
- ---------
115
-
116
- Copyright (c) 2010 Bruce Williams. See LICENSE for details.
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- spex execute scenarios.rb
@@ -1,5 +0,0 @@
1
- command "sudo chgrp everyone /tmp/foo"
2
-
3
- scenario :default, "Change group" do
4
- assert_chgrps_file '/tmp/foo', :to => 'everyone', :changes => true
5
- end
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- spex execute scenarios.rb
@@ -1,5 +0,0 @@
1
- command "chmod 700 /tmp/foo"
2
-
3
- scenario :default, "Change mode" do
4
- assert_chmods_file '/tmp/foo', :to => 0700, :changes => true
5
- end