sass-embedded 0.3.0 → 0.6.0

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.
data/lib/sass/util.rb CHANGED
@@ -1,20 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'pathname'
4
+
3
5
  module Sass
4
- # Utilities functions
6
+ # The {Util} module.
5
7
  module Util
6
8
  module_function
7
9
 
10
+ FILE_PROTOCOL = 'file://'
11
+
8
12
  def file_uri(path)
9
13
  absolute_path = File.absolute_path(path)
10
14
 
11
- unless absolute_path.start_with?('/')
15
+ unless absolute_path.start_with? File::SEPARATOR
12
16
  components = absolute_path.split File::SEPARATOR
13
- components[0] = components[0].split(':').first.downcase
17
+ components[0] = components[0][0].downcase
14
18
  absolute_path = components.join File::SEPARATOR
15
19
  end
16
20
 
17
- "file://#{absolute_path}"
21
+ "#{FILE_PROTOCOL}#{absolute_path}"
22
+ end
23
+
24
+ def path(file_uri)
25
+ absolute_path = file_uri[FILE_PROTOCOL.length..]
26
+
27
+ unless absolute_path.start_with? File::SEPARATOR
28
+ components = absolute_path.split File::SEPARATOR
29
+ components[0] = "#{components[0].upcase}:"
30
+ absolute_path = components.join File::SEPARATOR
31
+ end
32
+
33
+ absolute_path
34
+ end
35
+
36
+ def relative(from, to)
37
+ Pathname.new(File.absolute_path(to)).relative_path_from(Pathname.new(File.absolute_path(from))).to_s
18
38
  end
19
39
 
20
40
  def now
data/lib/sass/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sass
4
- VERSION = '0.3.0'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
21
 
22
- spec.required_ruby_version = '>= 2.6'
22
+ spec.required_ruby_version = '>= 2.6.0'
23
23
 
24
24
  spec.require_paths = ['lib']
25
25
 
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'test_helper'
4
+
5
+ module Sass
6
+ class ConcurrencyTest < MiniTest::Test
7
+ include TempFileTest
8
+
9
+ def setup
10
+ @embedded = Embedded.new
11
+ end
12
+
13
+ def teardown
14
+ @embedded.close
15
+ end
16
+
17
+ def render(data)
18
+ @embedded.render(data: data)[:css]
19
+ end
20
+
21
+ def test_concurrency
22
+ 10.times do
23
+ threads = []
24
+ 10.times do |i|
25
+ threads << Thread.new(i) do |id|
26
+ output = @embedded.render(data: "div { width: #{id} }")[:css]
27
+ assert_match(/#{id}/, output)
28
+ end
29
+ end
30
+ threads.each(&:join)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -12,6 +12,8 @@ module Sass
12
12
  @embedded.close
13
13
  end
14
14
 
15
+ # rubocop:disable Layout/LineLength
16
+
15
17
  def render(sass)
16
18
  @embedded.render(data: sass,
17
19
  functions: {
@@ -163,6 +165,8 @@ module Sass
163
165
  })[:css]
164
166
  end
165
167
 
168
+ # rubocop:enable Layout/LineLength
169
+
166
170
  def test_functions_may_return_sass_string_type
167
171
  assert_sass <<-SCSS, <<-CSS
168
172
  div { url: url(sass_return_path("foo.svg")); }
@@ -3,7 +3,7 @@
3
3
  require_relative 'test_helper'
4
4
 
5
5
  module Sass
6
- class CustomImporterTest < MiniTest::Test
6
+ class ImporterTest < MiniTest::Test
7
7
  include TempFileTest
8
8
 
9
9
  def setup
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'test_helper'
4
+
5
+ module Sass
6
+ class IncludePathsTest < MiniTest::Test
7
+ include TempFileTest
8
+
9
+ def setup
10
+ @embedded = Embedded.new
11
+ end
12
+
13
+ def teardown
14
+ @embedded.close
15
+ end
16
+
17
+ def test_include_paths
18
+ temp_dir('included_1')
19
+ temp_dir('included_2')
20
+
21
+ temp_file('included_1/import_parent.scss', '$s: 30px;')
22
+ temp_file('included_2/import.scss', "@use 'import_parent' as *; $size: $s;")
23
+ temp_file('styles.scss', "@use 'import.scss' as *; .hi { width: $size; }")
24
+
25
+ css = <<~CSS.chomp
26
+ .hi {
27
+ width: 30px;
28
+ }
29
+ CSS
30
+
31
+ assert_equal css, @embedded.render(file: 'styles.scss',
32
+ include_paths: %w[
33
+ included_1 included_2
34
+ ])[:css]
35
+ end
36
+
37
+ def test_global_include_paths
38
+ temp_dir('included_1')
39
+ temp_dir('included_2')
40
+
41
+ temp_file('included_1/import_parent.scss', '$s: 30px;')
42
+ temp_file('included_2/import.scss', "@use 'import_parent' as *; $size: $s;")
43
+ temp_file('styles.scss', "@use 'import.scss' as *; .hi { width: $size; }")
44
+
45
+ ::Sass.include_paths << 'included_1'
46
+ ::Sass.include_paths << 'included_2'
47
+
48
+ css = <<~CSS.chomp
49
+ .hi {
50
+ width: 30px;
51
+ }
52
+ CSS
53
+
54
+ assert_equal css, @embedded.render(file: 'styles.scss')[:css]
55
+ end
56
+
57
+ def test_include_paths_from_env
58
+ expected_include_paths = %w[included_3 included_4]
59
+
60
+ ::Sass.instance_eval { @include_paths = nil }
61
+
62
+ ENV['SASS_PATH'] = expected_include_paths.join(File::PATH_SEPARATOR)
63
+
64
+ assert_equal expected_include_paths, ::Sass.include_paths
65
+
66
+ ::Sass.include_paths.clear
67
+ end
68
+
69
+ def test_include_paths_not_configured
70
+ temp_dir('included_5')
71
+ temp_dir('included_6')
72
+ temp_file('included_5/import_parent.scss', '$s: 30px;')
73
+ temp_file('included_6/import.scss', "@use 'import_parent' as *; $size: $s;")
74
+ temp_file('styles.scss', "@use 'import.scss' as *; .hi { width: $size; }")
75
+
76
+ assert_raises(RenderError) do
77
+ @embedded.render(file: 'styles.scss')
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'test_helper'
4
+
5
+ module Sass
6
+ class IndentedSyntaxTest < MiniTest::Test
7
+ include TempFileTest
8
+
9
+ def setup
10
+ @embedded = Embedded.new
11
+ end
12
+
13
+ def teardown
14
+ @embedded.close
15
+ end
16
+
17
+ def test_input_data_with_indented_syntax
18
+ sass = <<~SASS
19
+ $size: 30px
20
+ .foo
21
+ width: $size
22
+ SASS
23
+
24
+ css = <<~CSS.chomp
25
+ .foo {
26
+ width: 30px;
27
+ }
28
+ CSS
29
+
30
+ assert_raises(RenderError) do
31
+ @embedded.render(data: sass)
32
+ end
33
+
34
+ assert_raises(RenderError) do
35
+ @embedded.render(data: sass, indented_syntax: false)
36
+ end
37
+
38
+ assert_equal css, @embedded.render(data: sass, indented_syntax: true)[:css]
39
+ end
40
+
41
+ def test_input_file_with_indented_syntax
42
+ sass = <<~SASS
43
+ $size: 30px
44
+ .foo
45
+ width: $size
46
+ SASS
47
+
48
+ css = <<~CSS.chomp
49
+ .foo {
50
+ width: 30px;
51
+ }
52
+ CSS
53
+
54
+ temp_file('style.sass', sass)
55
+
56
+ assert_equal css, @embedded.render(file: 'style.sass')[:css]
57
+ assert_equal css, @embedded.render(file: 'style.sass', indented_syntax: true)[:css]
58
+ assert_equal css, @embedded.render(file: 'style.sass', indented_syntax: false)[:css]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'test_helper'
4
+
5
+ module Sass
6
+ class InputTest < MiniTest::Test
7
+ include TempFileTest
8
+
9
+ def setup
10
+ @embedded = Embedded.new
11
+ end
12
+
13
+ def teardown
14
+ @embedded.close
15
+ end
16
+
17
+ def test_input_data
18
+ scss = <<~SCSS
19
+ $var: bang;
20
+
21
+ .foo {
22
+ baz: $var;
23
+ }
24
+ SCSS
25
+
26
+ css = <<~CSS.chomp
27
+ .foo {
28
+ baz: bang;
29
+ }
30
+ CSS
31
+
32
+ result = @embedded.render(data: scss)
33
+ assert_equal css, result[:css]
34
+ end
35
+
36
+ def test_input_file
37
+ scss = <<~SCSS
38
+ $var: bang;
39
+
40
+ .foo {
41
+ baz: $var;
42
+ }
43
+ SCSS
44
+
45
+ css = <<~CSS.chomp
46
+ .foo {
47
+ baz: bang;
48
+ }
49
+ CSS
50
+
51
+ temp_file('style.scss', scss)
52
+ result = @embedded.render(file: 'style.scss')
53
+ assert_equal css, result[:css]
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'test_helper'
4
+
5
+ module Sass
6
+ class OutputTest < MiniTest::Test
7
+ include TempFileTest
8
+
9
+ def setup
10
+ @embedded = Embedded.new
11
+ end
12
+
13
+ def teardown
14
+ @embedded.close
15
+ end
16
+
17
+ def test_output_output_style
18
+ data = <<~SCSS
19
+ $var: bang;
20
+
21
+ .foo {
22
+ baz: $var;
23
+ }
24
+ SCSS
25
+
26
+ assert_equal <<~CSS.chomp, @embedded.render(data: data, output_style: :expanded)[:css]
27
+ .foo {
28
+ baz: bang;
29
+ }
30
+ CSS
31
+
32
+ assert_equal <<~CSS.chomp, @embedded.render(data: data, output_style: :compressed)[:css]
33
+ .foo{baz:bang}
34
+ CSS
35
+
36
+ assert_raises(ArgumentError) do
37
+ @embedded.render(data: data, output_style: :nested)[:css]
38
+ end
39
+
40
+ assert_raises(ArgumentError) do
41
+ @embedded.render(data: data, output_style: :compact)[:css]
42
+ end
43
+
44
+ assert_raises(ArgumentError) do
45
+ @embedded.render(data: data, output_style: nil)[:css]
46
+ end
47
+ end
48
+
49
+ DATA_INDENT_TEST = <<~SCSS
50
+ @media all {
51
+ .foo {
52
+ baz: bang;
53
+ }
54
+ }
55
+ SCSS
56
+
57
+ def test_output_indent_width
58
+ assert_equal <<~CSS.chomp, @embedded.render(data: DATA_INDENT_TEST, indent_width: 0)[:css]
59
+ @media all {
60
+ .foo {
61
+ baz: bang;
62
+ }
63
+ }
64
+ CSS
65
+
66
+ assert_equal <<~CSS.chomp, @embedded.render(data: DATA_INDENT_TEST, indent_width: 1)[:css]
67
+ @media all {
68
+ .foo {
69
+ baz: bang;
70
+ }
71
+ }
72
+ CSS
73
+
74
+ assert_equal <<~CSS.chomp, @embedded.render(data: DATA_INDENT_TEST, indent_width: 4)[:css]
75
+ @media all {
76
+ .foo {
77
+ baz: bang;
78
+ }
79
+ }
80
+ CSS
81
+
82
+ assert_equal <<~CSS.chomp, @embedded.render(data: DATA_INDENT_TEST, indent_width: 10)[:css]
83
+ @media all {
84
+ .foo {
85
+ baz: bang;
86
+ }
87
+ }
88
+ CSS
89
+
90
+ assert_raises(RangeError) do
91
+ @embedded.render(data: DATA_INDENT_TEST, indent_width: -1)[:css]
92
+ end
93
+
94
+ assert_raises(RangeError) do
95
+ @embedded.render(data: DATA_INDENT_TEST, indent_width: 11)[:css]
96
+ end
97
+
98
+ assert_raises(ArgumentError) do
99
+ @embedded.render(data: DATA_INDENT_TEST, indent_width: 3.14)[:css]
100
+ end
101
+ end
102
+
103
+ def test_output_indent_type
104
+ assert_equal <<~CSS.chomp, @embedded.render(data: DATA_INDENT_TEST, indent_type: :tab)[:css]
105
+ @media all {
106
+ \t\t.foo {
107
+ \t\t\t\tbaz: bang;
108
+ \t\t}
109
+ }
110
+ CSS
111
+
112
+ assert_equal <<~CSS.chomp, @embedded.render(data: DATA_INDENT_TEST, indent_width: 1, indent_type: 'tab')[:css]
113
+ @media all {
114
+ \t.foo {
115
+ \t\tbaz: bang;
116
+ \t}
117
+ }
118
+ CSS
119
+ end
120
+
121
+ def test_output_linefeed
122
+ data = <<~SCSS
123
+ $var: bang;
124
+
125
+ .foo {
126
+ baz: $var;
127
+ }
128
+ SCSS
129
+
130
+ assert_equal ".foo {\n baz: bang;\n}", @embedded.render(data: data, linefeed: :lf)[:css]
131
+ assert_equal ".foo {\n\r baz: bang;\n\r}", @embedded.render(data: data, linefeed: :lfcr)[:css]
132
+ assert_equal ".foo {\r baz: bang;\r}", @embedded.render(data: data, linefeed: :cr)[:css]
133
+ assert_equal ".foo {\r\n baz: bang;\r\n}", @embedded.render(data: data, linefeed: :crlf)[:css]
134
+ end
135
+ end
136
+ end