File: test_theme_assets_reader.rb

package info (click to toggle)
jekyll 4.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,356 kB
  • sloc: ruby: 16,765; javascript: 1,455; sh: 214; xml: 29; makefile: 9
file content (102 lines) | stat: -rw-r--r-- 3,370 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require "helper"

class TestThemeAssetsReader < JekyllUnitTest
  def setup
    @site = fixture_site(
      "theme"       => "test-theme",
      "theme-color" => "black"
    )
    assert @site.theme
  end

  def assert_file_with_relative_path(haystack, relative_path)
    assert haystack.any? { |f|
      f.relative_path == relative_path
    }, "Site should read in the #{relative_path} file, but it was not found in #{haystack.inspect}"
  end

  def refute_file_with_relative_path(haystack, relative_path)
    refute haystack.any? { |f|
      f.relative_path == relative_path
    }, "Site should not have read in the #{relative_path} file, but it was found in " \
       "#{haystack.inspect}"
  end

  context "with a valid theme" do
    should "read all assets" do
      @site.reset
      ThemeAssetsReader.new(@site).read
      assert_file_with_relative_path @site.static_files, "/assets/img/logo.png"
      assert_file_with_relative_path @site.pages, "assets/style.scss"
    end

    should "convert pages" do
      @site.process

      file = @site.pages.find { |f| f.relative_path == "assets/style.scss" }
      refute_nil file
      assert_equal @site.in_dest_dir("assets/style.css"), file.destination(@site.dest)
      assert_includes file.output, ".sample {\n  color: black;\n}"
    end

    should "not overwrite site content with the same relative path" do
      @site.reset
      @site.read

      file = @site.pages.find { |f| f.relative_path == "assets/application.coffee" }
      static_script = File.read(
        @site.static_files.find { |f| f.relative_path == "/assets/base.js" }.path
      )
      refute_nil file
      refute_nil static_script
      assert_includes file.content, "alert \"From your site.\""
      assert_includes static_script, "alert(\"From your site.\");"
    end
  end

  context "with a valid theme without an assets dir" do
    should "not read any assets" do
      site = fixture_site("theme" => "test-theme")
      allow(site.theme).to receive(:assets_path).and_return(nil)
      ThemeAssetsReader.new(site).read
      refute_file_with_relative_path site.static_files, "/assets/img/logo.png"
      refute_file_with_relative_path site.pages, "assets/style.scss"
    end
  end

  context "with no theme" do
    should "not read any assets" do
      site = fixture_site("theme" => nil)
      ThemeAssetsReader.new(site).read
      refute_file_with_relative_path site.static_files, "/assets/img/logo.png"
      refute_file_with_relative_path site.pages, "assets/style.scss"
    end
  end

  context "symlinked theme" do
    should "not read assets from symlinked theme" do
      skip_if_windows "Jekyll does not currently support symlinks on Windows."

      begin
        tmp_dir = Dir.mktmpdir("jekyll-theme-test")
        File.binwrite(File.join(tmp_dir, "test.txt"), "content")

        theme_dir = File.join(__dir__, "fixtures", "test-theme-symlink")
        File.symlink(tmp_dir, File.join(theme_dir, "assets"))

        site = fixture_site(
          "theme"       => "test-theme-symlink",
          "theme-color" => "black"
        )
        ThemeAssetsReader.new(site).read

        assert_empty site.static_files, "static file should not have been picked up"
      ensure
        FileUtils.rm_rf(tmp_dir)
        FileUtils.rm_rf(File.join(theme_dir, "assets"))
      end
    end
  end
end