jib 0.0.1

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.
Files changed (3) hide show
  1. data/lib/jib.rb +195 -0
  2. data/spec/jib_spec.rb +137 -0
  3. metadata +47 -0
data/lib/jib.rb ADDED
@@ -0,0 +1,195 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+ require 'erb'
4
+
5
+ module Jib
6
+ def self.root
7
+ FileUtils.pwd
8
+ end
9
+
10
+ def self.workspace_path
11
+ File.join(root, ".jib-workspace")
12
+ end
13
+
14
+ def self.config
15
+ @config ||= Jib::Config.new
16
+ end
17
+
18
+ def self.env
19
+ @env ||= Env.new
20
+ end
21
+
22
+ def self.runner
23
+ @runner ||= Runner.new
24
+ end
25
+
26
+ def self.run
27
+ env.build
28
+ runner.run
29
+ # env.cleanup
30
+ end
31
+
32
+ class Runner
33
+ def run
34
+ Kernel.system("phantomjs #{path_to_runner_script} #{path_to_document}")
35
+ end
36
+
37
+ private
38
+
39
+ def path_to_runner_script
40
+ File.join(Jib.root, Jib.config.spec_path, "support", "jib", "run.coffee")
41
+ end
42
+
43
+ def path_to_document
44
+ File.join(Jib.workspace_path, "test-runner.html")
45
+ end
46
+ end
47
+
48
+ class Config
49
+ def sources
50
+ options['sources']
51
+ end
52
+
53
+ def source_path
54
+ options['source_path']
55
+ end
56
+
57
+ def specs
58
+ options['specs']
59
+ end
60
+
61
+ def spec_path
62
+ options['spec_path']
63
+ end
64
+
65
+ private
66
+
67
+ def options
68
+ @options ||= YAML.load(File.read(config_file))
69
+ end
70
+
71
+ def config_file
72
+ File.join(Jib.root, "spec", "javascript", "support", "jib.yml")
73
+ end
74
+ end
75
+
76
+ class Env
77
+ def build
78
+ Files.create_support
79
+ Compiler.new.compile
80
+ StaticFiles.new.copy
81
+ Document.new.write_to_file
82
+ end
83
+
84
+ def cleanup
85
+ FileUtils.rm_r(Jib.workspace_path)
86
+ end
87
+ end
88
+
89
+ class Files
90
+ def self.create_support
91
+ FileUtils.mkdir_p(
92
+ File.join(Jib.workspace_path, Jib.config.spec_path, "support")
93
+ )
94
+
95
+ FileUtils.cp_r(
96
+ File.join(Jib.root, Jib.config.spec_path, "support"),
97
+ File.join(Jib.workspace_path, Jib.config.spec_path)
98
+ )
99
+
100
+ end
101
+ end
102
+
103
+ class StaticFiles
104
+ def copy
105
+ copy_targets.each do |src, dest|
106
+ FileUtils.mkdir_p(File.dirname(dest))
107
+ Kernel.system("cp #{src} #{dest}")
108
+ end
109
+ end
110
+
111
+ def copy_targets
112
+ target(
113
+ Jib.config.sources.select {|src| File.extname(src) =~ /\.js|\.ejs/},
114
+ Jib.config.source_path
115
+ ) + target(
116
+ Jib.config.specs.select {|src| File.extname(src) =~ /\.js|\.ejs/},
117
+ Jib.config.spec_path
118
+ )
119
+ end
120
+
121
+ def target(sources, path)
122
+ sources.map do |filename|
123
+ [
124
+ File.join(Jib.root, path, filename),
125
+ File.join(File.join(Jib.workspace_path, path, filename))
126
+ ]
127
+ end
128
+ end
129
+ end
130
+
131
+ class Compiler
132
+ def compile
133
+ compile_targets.each do |src, dest|
134
+ Kernel.system("coffee -o #{dest} -c #{src}")
135
+ Kernel.exit(1) unless $? == 0
136
+ end
137
+ end
138
+
139
+ def compile_targets
140
+ target(compile_sources(Jib.config.sources), Jib.config.source_path) +
141
+ target(compile_sources(Jib.config.specs), Jib.config.spec_path)
142
+ end
143
+
144
+ def compile_sources(list)
145
+ list.select { |src| File.extname(src) == ".coffee" }
146
+ end
147
+
148
+ def target(sources, path)
149
+ sources.map do |filename|
150
+ [
151
+ File.join(Jib.root, path, filename),
152
+ File.dirname(File.join(Jib.workspace_path, path, filename))
153
+ ]
154
+ end
155
+ end
156
+ end
157
+
158
+ class Document
159
+ def template
160
+ ERB.new(
161
+ File.read(template_file_path)
162
+ ).result(self.get_binding)
163
+ end
164
+
165
+ def write_to_file
166
+ File.open(File.join(Jib.workspace_path, "test-runner.html"), "w+") { |file|
167
+ file << template
168
+ }
169
+ end
170
+
171
+ def sources
172
+ filenames(Jib.config.sources, Jib.config.source_path)
173
+ end
174
+
175
+ def specs
176
+ filenames(Jib.config.specs, Jib.config.spec_path)
177
+ end
178
+
179
+ def get_binding
180
+ binding
181
+ end
182
+
183
+ private
184
+
185
+ def filenames(collection, directory)
186
+ collection.map do |filename|
187
+ File.join(directory, filename.gsub(".coffee", ".js"))
188
+ end
189
+ end
190
+
191
+ def template_file_path
192
+ File.join(File.dirname(File.expand_path(__FILE__)), "template.erb")
193
+ end
194
+ end
195
+ end
data/spec/jib_spec.rb ADDED
@@ -0,0 +1,137 @@
1
+ require 'jib'
2
+
3
+ describe Jib do
4
+ it "has a root" do
5
+ FileUtils.stub(:pwd) { "working-dir" }
6
+ Jib.root.should eq "working-dir"
7
+ end
8
+
9
+ it "has a config" do
10
+ Jib.config.should be_a Jib::Config
11
+ end
12
+
13
+ context "#workspace_path" do
14
+ it "is root + .jib-workspace" do
15
+ FileUtils.stub(:pwd) { "working-dir" }
16
+ Jib.workspace_path.should eq "working-dir/.jib-workspace"
17
+ end
18
+ end
19
+ end
20
+
21
+ describe Jib::Config do
22
+ let(:config) { Jib::Config.new }
23
+
24
+ before do
25
+ File.stub(:read)
26
+
27
+ YAML.stub(:load) do
28
+ {
29
+ 'sources' => ['models/example.coffee'],
30
+ 'specs' => ["models/exampleSpec.coffee"],
31
+ 'source_path' => "public/assets/javascripts",
32
+ 'spec_path' => 'spec/javascripts'
33
+ }
34
+ end
35
+ end
36
+
37
+ it "has a list of sources" do
38
+ config.sources.should eq ['models/example.coffee']
39
+ end
40
+
41
+ it "has a list of specs" do
42
+ config.specs.should eq ["models/exampleSpec.coffee"]
43
+ end
44
+
45
+ it "has a path (relative to root) for sources" do
46
+ config.source_path.should eq "public/assets/javascripts"
47
+ end
48
+
49
+ it "has a path (relative to root) for specs" do
50
+ config.spec_path.should eq "spec/javascripts"
51
+ end
52
+ end
53
+
54
+ describe Jib::Env do
55
+ let(:env) { Jib::Env.new }
56
+ let(:compiler) { stub }
57
+ let(:document) { stub }
58
+
59
+ before do
60
+ FileUtils.stub(:mkdir_p)
61
+ FileUtils.stub(:cp_r)
62
+
63
+ Jib.config.stub(:options) {
64
+ {
65
+ 'sources' => [],
66
+ 'specs' => [],
67
+ 'source_path' => '',
68
+ 'spec_path' => ''
69
+ }
70
+ }
71
+ end
72
+
73
+ it "makes the directory" do
74
+ Jib::Compiler.stub(:new) { compiler }
75
+ Jib::Document.stub(:new) { document }
76
+ Jib::Files.should_receive(:create_support)
77
+ compiler.should_receive(:compile)
78
+ document.should_receive(:write_to_file)
79
+
80
+
81
+ env.build
82
+ end
83
+
84
+ it "compiles coffee sources" do
85
+ Jib::Compiler.stub(:new) { compiler }
86
+ Jib::Document.stub(:new) { document }
87
+
88
+ compiler.should_receive(:compile)
89
+ document.should_receive(:write_to_file)
90
+
91
+ env.build
92
+ end
93
+
94
+
95
+ end
96
+
97
+ describe Jib::Compiler do
98
+ let(:compiler) { Jib::Compiler.new }
99
+
100
+ before do
101
+ Jib.config.stub(:options) do
102
+ {
103
+ 'sources' => ['models/example.coffee'],
104
+ 'specs' => ["models/exampleSpec.coffee"],
105
+ 'source_path' => "public/javascripts",
106
+ 'spec_path' => 'spec/javascripts'
107
+ }
108
+ end
109
+
110
+ Jib.stub(:root) { "root" }
111
+ end
112
+
113
+ it "#build compiles the stuff to the stuff" do
114
+ Kernel.stub(:exit)
115
+
116
+ compiler.compile_targets.each do |src, dest|
117
+ Kernel.should_receive(:system).with(
118
+ "coffee -o #{dest} -c #{src}"
119
+ ).once
120
+ end
121
+
122
+ compiler.compile
123
+ end
124
+
125
+ it "#compile_targets specifies the sources/output path" do
126
+ compiler.compile_targets.should eq [
127
+ [
128
+ "root/public/javascripts/models/example.coffee",
129
+ "root/.jib-workspace/public/javascripts/models"
130
+ ],
131
+ [
132
+ "root/spec/javascripts/models/exampleSpec.coffee",
133
+ "root/.jib-workspace/spec/javascripts/models"
134
+ ]
135
+ ]
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Pratt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: The small sail in the front of a boat.
15
+ email: brian@8thlight.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/jib.rb
21
+ - spec/jib_spec.rb
22
+ homepage: http://8thlight.com
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.24
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Jib - a test runner for phantomJS that doesn't piss me off
46
+ test_files:
47
+ - spec/jib_spec.rb