File: jekyll.rb

package info (click to toggle)
ruby-jekyll-polyglot 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,196 kB
  • sloc: xml: 1,500; ruby: 947; sh: 10; makefile: 4
file content (182 lines) | stat: -rw-r--r-- 6,480 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# ----------------------------------------------------------------------------
# Frozen-string-literal: true
# Copyright: 2012 - 2016 - MIT License
# Encoding: utf-8
# ----------------------------------------------------------------------------

require "nokogiri"

module Jekyll
  module RSpecHelpers
    class ContextThief < Liquid::Tag
      class << self
        attr_accessor :context
      end

      # ----------------------------------------------------------------------

      def render(context)
        self.class.context = context
      end
    end

    # ------------------------------------------------------------------------
    # Allows you to get a real context context, to test methods directly
    # if you need to, most can be tested indirectly.
    # ------------------------------------------------------------------------

    def build_context
      site = stub_jekyll_site
      site.liquid_renderer.file("").parse("{% context_thief %}") \
        .render!(site.site_payload, :registers => { :site => site })
      ContextThief.context
    end

    # ------------------------------------------------------------------------

    def fragment(html)
      Nokogiri::HTML.fragment(html)
    end

    # ------------------------------------------------------------------------
    # Allows you to run something capturing all errors and output so you can
    # run a test cleanly without a bunch of noise, nobody likes noise when they
    # are just trying to see the result of a failed test.
    # ------------------------------------------------------------------------

    def silence_stdout(return_stringio = false)
      old_stdout = $stdout; old_stderr = $stderr
      $stdout = stdout = StringIO.new
      $stderr = stderr = StringIO.new
      output  = yield

      if return_stringio
        return [
          stdout.string,
          stderr.string
        ]
      else
        return output
      end
    ensure
      $stdout = old_stdout
      $stderr = old_stderr
    end

    # ------------------------------------------------------------------------
    # Uses `#silence_stdout` to return output to you without you having
    # to use true or false on `#silence_stoudt`.
    # ------------------------------------------------------------------------

    def capture_stdout(&block)
      silence_stdout true, &block
    end

    # ------------------------------------------------------------------------
    # Stubs the asset config on Jekyll for you, this is meant to be used
    # in a new context or at the top of a context so that you can stub the
    # configuration and have it reset afterwards.
    # ------------------------------------------------------------------------

    def stub_asset_config(inst, hash = nil)
      (hash = inst; inst = nil) if inst.is_a?(Hash)
      inst = @site || site unless inst

      hash = Jekyll::Assets::Config.merge(hash)
      allow(inst).to receive(:config).and_return(inst.config.merge({
        "assets" => hash
      }))
    end

    # ------------------------------------------------------------------------

    def stub_env_config(inst, hash = nil)
      (hash = inst; inst = nil) if inst.is_a?(Hash)
      inst = @env || env unless inst

      hash = Jekyll::Utils.deep_merge_hashes(inst.asset_config, hash)
      allow(inst).to receive(:asset_config).and_return(hash)
    end

    # ------------------------------------------------------------------------
    # Strips ANSI from the output so that you can test just a plain text
    # string without worrying about whether colors are there or not, this is
    # mostly useful for testing log output or other helpers.
    # ------------------------------------------------------------------------

    def strip_ansi(str)
      str.gsub(/\e\[(?:\d+)(?:;\d+)?m/, "")
    end

    # ------------------------------------------------------------------------
    # Stubs the Jekyll site with your configuration, most of the time you
    # won't do it this way though, because you can just initialize the default
    # configuration with our merges and stub the configuration pieces.
    # ------------------------------------------------------------------------

    def stub_jekyll_site(opts = {})
      path = File.expand_path("../../fixture", __FILE__)
      Jekyll::RSpecHelpers.cleanup_trash

      silence_stdout do
        Jekyll::Site.new(Jekyll.configuration(opts.merge({
          "source" => path, "destination" => File.join(path, "_site")
        }))).tap(&:read)
      end
    end

    # ------------------------------------------------------------------------
    # See: `#stub_jekyll_site` except this also kicks a process so that
    #   do both the building and the processing all in one shot.
    # ------------------------------------------------------------------------

    def stub_jekyll_site_with_processing(oth_opts = {})
      site = stub_jekyll_site(oth_opts)
      silence_stdout { site.process }
      site
    end

    # ------------------------------------------------------------------------
    # Pulls a file and passes the data to you (from _site), this does
    # no checking so it will raise an error if there is no file that you wish
    # to pull, so beware of that when testing.
    # ------------------------------------------------------------------------

    def get_stubbed_file(file)
      path = File.expand_path("../../fixture/_site", __FILE__)
      File.read(File.join(path, file))
    end

    # ------------------------------------------------------------------------
    # Cleanup after ourselves when testing, removing the data that we and
    # through us Jekyll created, so that if we test again there is nothing but
    # clean data to test with, and not dirty old data.
    # ------------------------------------------------------------------------

    def self.cleanup_trash
      %W(.asset-cache .jekyll-metadata _site).each do |v|
        FileUtils.rm_rf(File.join(File.expand_path("../../fixture", __FILE__), v))
      end
    end
  end
end

# ----------------------------------------------------------------------------

Liquid::Template.register_tag "context_thief", \
  Jekyll::RSpecHelpers::ContextThief

# ----------------------------------------------------------------------------

RSpec.configure do |c|
  c.include Jekyll::RSpecHelpers
  c.before :all do
    Jekyll::RSpecHelpers.cleanup_trash
  end

  #

  c.after :all do
    Jekyll::RSpecHelpers.cleanup_trash
  end
end