sitespec 0.0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77d72de6ad811ebe5d8ec22b950095fd12bdc010
4
- data.tar.gz: 257c93ecebde01574fa958e1b2fc3c42ba0b6f97
3
+ metadata.gz: d9fc89ff9faa0578468ba10193d3c0ce7503a2c9
4
+ data.tar.gz: 5de14be6b1383849c8027a460eb1a6815be8f239
5
5
  SHA512:
6
- metadata.gz: 09f1380c36ee72d02649fcb5c4272e29d10d5c1e5f99cdae2ef19bc18a202f52d906f92d61c2c366706290bfe4920fbea217489e93be8a27cd1898d93188437d
7
- data.tar.gz: d4a531f5208a4402b66e1e4aee61808ed64179808228052d8957523714ae93fb60a35d9991e3b9a5d03976b2184ab8dbb96f31fef4fbeffe8f8f9854ab715fe1
6
+ metadata.gz: 0dcf588bd912f06ec9a57677e76ac66684fcd8afbb91de6f1d8e097bec25dcde54b53bbc75cc90d425c903cc9f85ffa7e3cfafc26b5e02d0745ce42359ec6593
7
+ data.tar.gz: 431a14bb5c21985f7be8154c985f22e1efb2b84eca0210bd1a159cbda6caee36bdd2b12b923ffb2a4f2379469fe89ab96e5fc8d317de7c18e3caeb3cdd1962a5
data/CHANGELOG.md CHANGED
@@ -1,15 +1,18 @@
1
+ ## 1.0.0
2
+ - Renewal with Ruby 2, RSpec 3, and rack-test
3
+
1
4
  ## 0.0.5
2
- * Fix dependent RSpec version
5
+ - Fix dependent RSpec version
3
6
 
4
7
  ## 0.0.4
5
- * Support Ruby 1.9.x
8
+ - Support Ruby 1.9.x
6
9
 
7
10
  ## 0.0.3
8
- * Fix rspec gem dependency
11
+ - Fix rspec gem dependency
9
12
 
10
13
  ## 0.0.2
11
- * Pretty RSpec formatter
14
+ - Pretty RSpec formatter
12
15
 
13
16
  ## 0.0.1
14
- * Basic features to generate files from request spec
15
- * The 1st release on 2013-11-15
17
+ - Basic features to generate files from request spec
18
+ - The 1st release on 2013-11-15
data/README.md CHANGED
@@ -1,56 +1,77 @@
1
1
  # Sitespec
2
2
  Generate static site from your rack application & spec definition.
3
3
 
4
- ## Features
5
4
  * Provides the same way to create both dynamic & static website
6
5
  * Generates static website from your existing dynamic website
7
6
  * Sitespec can be executable specification, good documentation, and well-tested implementation
8
7
 
9
- ## Installation
10
- ```sh
11
- # shell-command
12
- gem install sitespec
8
+ ## Usage
9
+ ### Add sitespec into your Gemfile
10
+ ```rb
11
+ # Gemfile
12
+ gem "sitespec"
13
13
  ```
14
14
 
15
- ## Usage
16
- ```ruby
17
- # spec/your_spec.rb
18
- require "sitespec"
19
-
20
- Sitespec.configure do
21
- self.application = MyRackApplication.new
22
- self.build_path = "build"
23
- self.raise_http_error = true
24
- end
15
+ ### Require sitespec/rspec in your specs
16
+ ```rb
17
+ # spec/spec_helper.rb
18
+ require "sitespec/rspec"
19
+ ```
20
+
21
+ ### Write request-specs with `:sitespec` metadata
22
+ Note: [rack/test](https://github.com/brynary/rack-test) is automatically enabled
23
+ in the example groups that have `:sitespec`.
25
24
 
25
+ ```rb
26
+ # spec/site_spec.rb
26
27
  describe "Sitespec" do
27
- include Sitespec
28
-
29
- it "generates static site from your rack application & spec definition" do
30
- get "/2000/01/01/hello.html"
31
- get "/2000/01/02/world.html"
32
- get "/feed.xml"
33
- get "/images/favicon.ico"
34
- get "/index.html"
35
- get "/stylesheets/all.css"
28
+ let(:app) do
29
+ MyRackApp
30
+ end
31
+
32
+ %w[
33
+ /2000/01/01/hello.html
34
+ /2000/01/02/world.html
35
+ /feed.xml
36
+ /images/favicon.ico
37
+ /index.html
38
+ /stylesheets/all.css
39
+ ].each do |path|
40
+ describe "GET #{path}", :sitespec do
41
+ it "returns 200" do
42
+ expect(get(path).status).to eq 200
43
+ end
44
+ end
36
45
  end
37
46
  end
38
47
  ```
39
48
 
40
- ```sh
41
- # shell-command
42
- $ rspec
43
- Build started...
49
+ ### Run rspec to build static files
50
+ Note: only successful examples generate static files.
44
51
 
45
- ✔ 2000/01/01/hello.html
46
- 2000/01/02/world.html
47
- ✔ feed.xml
48
- ✔ images/favicon.ico
49
- ✔ index.html
50
- ✔ stylesheets/all.css
52
+ ```
53
+ $ bundle exec rspec
51
54
 
52
- Build finished with 6 files in 0.00151 seconds.
55
+ Example application
56
+ GET /2000-01-01-hello.html
57
+ returns 200
58
+ GET /index.html
59
+ returns 200
60
+ GET /stylesheets/all.css
61
+ returns 200
62
+
63
+ Finished in 0.08302 seconds (files took 0.79161 seconds to load)
64
+ 3 examples, 0 failures
53
65
  ```
54
66
 
55
- ## Example
56
- [r7kamura/r7kamura.github.io](https://github.com/r7kamura/r7kamura.github.io) is powered by sitespec.
67
+ ## Configuration
68
+ - `Sitespec.configuration.build_path` - Where to locate files (default: `"build"`)
69
+ - `Sitespec.configuration.enabled` - Flag to enable sitespec (default: `true`)
70
+
71
+ ## Advanced topics
72
+ Sitespec is excellent with GitHub Pages.
73
+ [r7kamura/r7kamura.github.io](https://github.com/r7kamura/r7kamura.github.io)
74
+ is a working example that uses Sitespec to build static files from Rack application.
75
+ It uses TravisCI to build and push files to GitHub repo's master branch.
76
+ See [.travis.yml](https://github.com/r7kamura/r7kamura.github.io/blob/source/.travis.yml)
77
+ for more information about how to to it.
data/lib/sitespec.rb CHANGED
@@ -1,52 +1,29 @@
1
- require "sitespec/builder"
1
+ require "sitespec/artifact"
2
2
  require "sitespec/configuration"
3
- require "sitespec/error"
4
- require "sitespec/formatter"
5
- require "sitespec/request"
6
- require "sitespec/response"
7
3
  require "sitespec/version"
8
- require "sitespec/writer"
9
- require "sitespec/initialize"
10
4
 
11
5
  module Sitespec
12
6
  class << self
13
- attr_writer :build_count
7
+ attr_writer :artifacts_count
14
8
 
15
- def configuration
16
- @configuration ||= Configuration.new
9
+ def artifacts_count
10
+ @artifacts_count || 0
17
11
  end
18
12
 
19
- def configure(&block)
20
- configuration.instance_eval(&block)
13
+ # Provide singleton configuration object shared with the current process.
14
+ # @example
15
+ # Sitespec.configuration.build_path = "artifacts"
16
+ def configuration
17
+ @configuration ||= Sitespec::Configuration.new
21
18
  end
22
19
 
23
- def build_count
24
- @build_count ||= 0
20
+ # Provide friendly interface to access to Sitespec.configuration.
21
+ # @example
22
+ # Sitespec.configure do |configuration|
23
+ # configuration.build_path = "artifacts"
24
+ # end
25
+ def configure(&block)
26
+ block.call(configuration)
25
27
  end
26
28
  end
27
-
28
- module_function
29
-
30
- attr_reader :request, :response
31
-
32
- def get(path, params = {}, env = {})
33
- process(:get, path, params, env)
34
- end
35
-
36
- def post(path, params = {}, env = {})
37
- process(:post, path, params, env)
38
- end
39
-
40
- def put(path, params = {}, env = {})
41
- process(:put, path, params, env)
42
- end
43
-
44
- def delete(path, params = {}, env = {})
45
- process(:delete, path, params, env)
46
- end
47
-
48
- def process(*args)
49
- @request = Request.new(*args)
50
- @response = Builder.build(@request)
51
- end
52
29
  end
@@ -0,0 +1,89 @@
1
+ require "pathname"
2
+ require "sitespec"
3
+
4
+ module Sitespec
5
+ # "Artifact" is corresponding to each response body to an endpoint.
6
+ # This class has ActiveRecord-like interface like Sitespec::Artifact.create.
7
+ #
8
+ # @example
9
+ # Sitespec::Artifact.create(example: example, example_group: example_group)
10
+ #
11
+ class Artifact
12
+ class << self
13
+ # Short-hand method to call `.new` and `#save`
14
+ # @return [Sitespec::Artifact]
15
+ def create(**args)
16
+ new(**args).tap(&:save)
17
+ end
18
+ end
19
+
20
+ # @param [RSpec::Core::Example] example In almost cases, `RSpec.current_example` is passed
21
+ # @param [RSpec::Core::ExampleGroup] example_group In almost cases, `self` is passed
22
+ def initialize(example: nil, example_group: nil)
23
+ @example = example
24
+ @example_group = example_group
25
+ end
26
+
27
+ # Validate its attributes and generate a new file
28
+ # @return [false, true] True for successful case, otherwise false
29
+ def save
30
+ if valid?
31
+ write
32
+ count_up
33
+ true
34
+ else
35
+ false
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def count_up
42
+ Sitespec.artifacts_count += 1
43
+ end
44
+
45
+ # Utility method to access to `@example`
46
+ def example
47
+ @example
48
+ end
49
+
50
+ # Utility method to access to `@example_group`
51
+ def example_group
52
+ @example_group
53
+ end
54
+
55
+ def generate_file
56
+ pathname.write(response.body)
57
+ end
58
+
59
+ def make_parent_directory
60
+ pathname.parent.mkpath
61
+ end
62
+
63
+ # @return [Pathname] Where to an artifact file is located
64
+ def pathname
65
+ Pathname.new(File.join(Sitespec.configuration.build_pathname, request.path))
66
+ end
67
+
68
+ # @note We expect `request` is provided via rack-test
69
+ def request
70
+ example_group.last_request
71
+ end
72
+
73
+ # @note We expect `response` is provided via rack-test
74
+ def response
75
+ example_group.last_response
76
+ end
77
+
78
+ # @return [false, true] Validation result
79
+ def valid?
80
+ example.exception.nil? && Sitespec.configuration.enabled?
81
+ end
82
+
83
+ # Generate directory and file
84
+ def write
85
+ make_parent_directory
86
+ generate_file
87
+ end
88
+ end
89
+ end
@@ -1,10 +1,35 @@
1
+ require "pathname"
2
+
1
3
  module Sitespec
2
4
  class Configuration
3
- attr_accessor :application, :build_path, :raise_http_error
5
+ DEFAULT_BUILD_PATH = "build"
6
+
7
+ attr_writer :build_path
4
8
 
5
9
  def initialize
6
- @build_path = "build"
7
- @raise_http_error = true
10
+ @enabled = true
11
+ end
12
+
13
+ def build_path
14
+ @build_path || DEFAULT_BUILD_PATH
15
+ end
16
+
17
+ # @return [Pathname]
18
+ def build_pathname
19
+ Pathname.new(build_path)
20
+ end
21
+
22
+ def disable
23
+ @enabled = false
24
+ end
25
+
26
+ def enable
27
+ @enabled = true
28
+ end
29
+
30
+ def enabled
31
+ !!@enabled
8
32
  end
33
+ alias_method :enabled?, :enabled
9
34
  end
10
35
  end
@@ -0,0 +1,20 @@
1
+ # This file does not provide any class or module named Sitespec::RSpec,
2
+ # but configures suitable settings to run Sitespec with RSpec.
3
+
4
+ require "rack/test"
5
+ require "rspec"
6
+ require "sitespec"
7
+
8
+ RSpec.configure do |config|
9
+ config.include Rack::Test::Methods, sitespec: true
10
+
11
+ config.after do |example|
12
+ Sitespec::Artifact.create(example: example, example_group: self)
13
+ end
14
+
15
+ config.after(:suite) do
16
+ if Sitespec.artifacts_count > 0
17
+ puts "\nSitespec generated #{Sitespec.artifacts_count} files into #{Sitespec.configuration.build_path} directory."
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Sitespec
2
- VERSION = "0.0.5"
2
+ VERSION = "1.0.0"
3
3
  end
data/sitespec.gemspec CHANGED
@@ -10,13 +10,14 @@ Gem::Specification.new do |spec|
10
10
  spec.summary = "Generate static site from your rack application & spec definition"
11
11
  spec.homepage = "https://github.com/r7kamura/sitespec"
12
12
  spec.license = "MIT"
13
-
14
13
  spec.files = `git ls-files`.split($/)
15
14
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
15
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
16
  spec.require_paths = ["lib"]
17
+ spec.required_ruby_version = ">= 2.0.0"
18
18
 
19
- spec.add_dependency "rspec", "~> 2.14.1"
19
+ spec.add_dependency "rack-test"
20
+ spec.add_dependency "rspec", ">= 3.0.0"
20
21
  spec.add_development_dependency "bundler"
21
22
  spec.add_development_dependency "padrino"
22
23
  spec.add_development_dependency "pry"
@@ -1,9 +1,19 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Sitespec do
4
- it "generates static files from rack application" do
5
- get "/stylesheets/all.css"
6
- get "/index.html"
7
- get "/2000-01-01-hello.html"
3
+ describe "Example application" do
4
+ let(:app) do
5
+ Sitespec::ExampleApplication
6
+ end
7
+
8
+ %w[
9
+ /2000-01-01-hello.html
10
+ /index.html
11
+ /stylesheets/all.css
12
+ ].each do |path|
13
+ describe "GET #{path}", :sitespec do
14
+ it "returns 200" do
15
+ expect(get(path).status).to eq 200
16
+ end
17
+ end
8
18
  end
9
19
  end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,3 @@
1
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
- require "sitespec"
1
+ require "sitespec/rspec"
3
2
 
4
3
  Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f }
5
-
6
- RSpec.configure do |config|
7
- config.treat_symbols_as_metadata_keys_with_true_values = true
8
- config.include Sitespec
9
- end
10
-
11
- Sitespec.configuration.application = Sitespec::ExampleApplication
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sitespec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-30 00:00:00.000000000 Z
11
+ date: 2015-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack-test
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - "~>"
31
+ - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: 2.14.1
33
+ version: 3.0.0
20
34
  type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - "~>"
38
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: 2.14.1
40
+ version: 3.0.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -136,15 +150,10 @@ files:
136
150
  - README.md
137
151
  - Rakefile
138
152
  - lib/sitespec.rb
139
- - lib/sitespec/builder.rb
153
+ - lib/sitespec/artifact.rb
140
154
  - lib/sitespec/configuration.rb
141
- - lib/sitespec/error.rb
142
- - lib/sitespec/formatter.rb
143
- - lib/sitespec/initialize.rb
144
- - lib/sitespec/request.rb
145
- - lib/sitespec/response.rb
155
+ - lib/sitespec/rspec.rb
146
156
  - lib/sitespec/version.rb
147
- - lib/sitespec/writer.rb
148
157
  - sitespec.gemspec
149
158
  - spec/sitespec_spec.rb
150
159
  - spec/spec_helper.rb
@@ -167,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
167
176
  requirements:
168
177
  - - ">="
169
178
  - !ruby/object:Gem::Version
170
- version: '0'
179
+ version: 2.0.0
171
180
  required_rubygems_version: !ruby/object:Gem::Requirement
172
181
  requirements:
173
182
  - - ">="
@@ -175,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
184
  version: '0'
176
185
  requirements: []
177
186
  rubyforge_project:
178
- rubygems_version: 2.2.2
187
+ rubygems_version: 2.4.5
179
188
  signing_key:
180
189
  specification_version: 4
181
190
  summary: Generate static site from your rack application & spec definition
@@ -189,4 +198,3 @@ test_files:
189
198
  - spec/support/application/views/index.slim
190
199
  - spec/support/application/views/layouts/application.slim
191
200
  - spec/support/application/views/show.slim
192
- has_rdoc:
@@ -1,23 +0,0 @@
1
- module Sitespec
2
- class Builder
3
- def self.build(*args)
4
- new(*args).build
5
- end
6
-
7
- def initialize(request)
8
- @request = request
9
- end
10
-
11
- def build
12
- Response.new(*call).tap {|response| Writer.write(@request, response) }
13
- end
14
-
15
- def call
16
- Sitespec.configuration.application.call(@request.env).tap do |status, header, body|
17
- if Sitespec.configuration.raise_http_error && status >= 400
18
- raise Error, "#{@request.uri} returned #{status}"
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,4 +0,0 @@
1
- module Sitespec
2
- class Error < StandardError
3
- end
4
- end
@@ -1,19 +0,0 @@
1
- require "rspec/core/formatters/documentation_formatter"
2
-
3
- module Sitespec
4
- class Formatter < RSpec::Core::Formatters::BaseTextFormatter
5
- def start(*)
6
- super
7
- output.puts "Build started...\n\n"
8
- end
9
-
10
- def dump_summary(duration, example_count, failure_count, pending_count)
11
- @duration = duration
12
- @example_count = example_count
13
- @failure_count = failure_count
14
- @pending_count = pending_count
15
- output.puts "\nBuild finished with #{Sitespec.build_count} files in #{format_duration(duration)}.\n"
16
- dump_commands_to_rerun_failed_examples
17
- end
18
- end
19
- end
@@ -1,2 +0,0 @@
1
- RSpec.configuration.formatters.clear
2
- RSpec.configuration.formatter = Sitespec::Formatter
@@ -1,40 +0,0 @@
1
- require "stringio"
2
- require "uri"
3
-
4
- module Sitespec
5
- class Request
6
- def initialize(method, path, params = {}, custom_env = {})
7
- @custom_env = custom_env
8
- @method = method
9
- @params = params
10
- @path = path
11
- end
12
-
13
- def env
14
- @env ||= {
15
- "HTTPS" => https? ? "on" : "off",
16
- "HTTP_HOST" => uri.host,
17
- "PATH_INFO" => uri.path,
18
- "QUERY_STRING" => uri.query || "",
19
- "REQUEST_METHOD" => @method.to_s.upcase,
20
- "REQUEST_URI" => uri.to_s,
21
- "SERVER_NAME" => uri.hostname,
22
- "SERVER_PORT" => uri.port,
23
- "rack.errors" => StringIO.new,
24
- "rack.input" => StringIO.new,
25
- "rack.multithread" => false,
26
- "rack.run_once" => false,
27
- "rack.url_scheme" => https? ? "http" : "https",
28
- "rack.version" => [1, 1],
29
- }.merge(@custom_env)
30
- end
31
-
32
- def uri
33
- @uri ||= URI.parse(@path)
34
- end
35
-
36
- def https?
37
- URI::HTTP === uri && uri.https?
38
- end
39
- end
40
- end
@@ -1,17 +0,0 @@
1
- module Sitespec
2
- class Response
3
- attr_reader :status, :header
4
-
5
- def initialize(status, header, body)
6
- @body = body
7
- @header = header
8
- @status = status
9
- end
10
-
11
- def body
12
- lines = []
13
- @body.each {|line| lines << line }
14
- lines.join
15
- end
16
- end
17
- end
@@ -1,46 +0,0 @@
1
- # coding: utf-8
2
- module Sitespec
3
- class Writer
4
- def self.write(*args)
5
- new(*args).write
6
- end
7
-
8
- def initialize(request, response)
9
- @request = request
10
- @response = response
11
- end
12
-
13
- def write
14
- mkpath
15
- create_with_output_and_build_count
16
- end
17
-
18
- def path
19
- @path ||= Pathname.new("#{Sitespec.configuration.build_path}#{@request.uri.path}")
20
- end
21
-
22
- def mkpath
23
- path.parent.mkpath
24
- end
25
-
26
- def create
27
- path.open("w") {|file| file << @response.body }
28
- end
29
-
30
- def create_with_output
31
- create.tap { puts "#{color(?✔)} #{path}" }
32
- end
33
-
34
- def create_with_output_and_build_count
35
- create_with_output.tap { Sitespec.build_count += 1 }
36
- end
37
-
38
- def color(text)
39
- RSpec.configuration.color_enabled? ? "\e[32m#{text}\e[0m" : text
40
- end
41
-
42
- def puts(text)
43
- RSpec.configuration.formatters[0].output.puts text
44
- end
45
- end
46
- end