Skip to content

Commit 611ac1e

Browse files
Christophe PopovChristophe Popov
Christophe Popov
authored and
Christophe Popov
committed
marceloverdijk#22 Single or Double Quotes around imports
1 parent 9e96374 commit 611ac1e

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
1+
.idea/
22
.classpath
33
.project
44
.settings/
5+
*.iml
6+
*.ipr
57
target/

src/main/java/org/lesscss/LessSource.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class LessSource {
3434
/**
3535
* The <code>Pattern</code> used to match imported files.
3636
*/
37-
private static final Pattern IMPORT_PATTERN = Pattern.compile("^(?!\\s*//\\s*)@import\\s+(url\\()?\\s*\"(.+)\\s*\"(\\))?\\s*;.*$", MULTILINE);
38-
37+
private static final Pattern IMPORT_PATTERN = Pattern.compile("^(?!\\s*//\\s*)@import\\s+(url\\()?\\s*(\"|')(.+)\\s*(\"|')(\\))?\\s*;.*$", MULTILINE);
38+
3939
private File file;
4040
private String content;
4141
private String normalizedContent;
@@ -103,7 +103,7 @@ public String getNormalizedContent() {
103103
public long getLastModified() {
104104
return file.lastModified();
105105
}
106-
106+
107107
/**
108108
* Returns the time that the LESS source, or one of its imports, was last modified.
109109
*
@@ -134,11 +134,11 @@ public long getLastModifiedIncludingImports() {
134134
public Map<String, LessSource> getImports() {
135135
return imports;
136136
}
137-
137+
138138
private void resolveImports() throws FileNotFoundException, IOException {
139139
Matcher importMatcher = IMPORT_PATTERN.matcher(normalizedContent);
140140
while (importMatcher.find()) {
141-
String importedFile = importMatcher.group(2);
141+
String importedFile = importMatcher.group(3);
142142
importedFile = importedFile.matches(".*\\.(le?|c)ss$") ? importedFile : importedFile + ".less";
143143
boolean css = importedFile.matches(".*css$");
144144
if (!css) {

src/test/java/integration/ImportIT.java

+5
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public void testImport() throws Exception {
2727
public void testImportEndsInLess() throws Exception {
2828
testCompile(toFile("import/endsinless/less/import.less"), toFile("import/endsinless/css/import.css"));
2929
}
30+
31+
@Test
32+
public void testImportSingleQuotes() throws Exception {
33+
testCompile(toFile("import/less/import_quotes.less"), toFile("import/css/import.css"));
34+
}
3035
}

src/test/java/org/lesscss/LessSourceTest.java

+20-22
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import static org.powermock.api.mockito.PowerMockito.when;
2121
import java.io.File;
2222
import java.io.FileNotFoundException;
23+
import java.io.IOException;
2324
import java.util.LinkedHashMap;
2425
import java.util.Map;
2526
import org.apache.commons.io.FileUtils;
2627
import org.apache.commons.lang3.reflect.FieldUtils;
2728
import org.junit.Before;
29+
import org.junit.Ignore;
2830
import org.junit.Test;
2931
import org.junit.runner.RunWith;
3032
import org.mockito.Mock;
@@ -40,15 +42,13 @@ public class LessSourceTest {
4042
private LessSource lessSource;
4143

4244
@Mock private File file;
43-
45+
4446
@Mock private LessSource import1;
4547
@Mock private LessSource import2;
4648
@Mock private LessSource import3;
4749

4850
private Map<String, LessSource> imports;
49-
50-
private String content = "content";
51-
private String absolutePath = "path";
51+
5252
private long lastModified = 1l;
5353

5454
@Before
@@ -61,17 +61,13 @@ public void setUp() throws Exception {
6161

6262
@Test
6363
public void testNewLessSourceWithoutImports() throws Exception {
64-
when(file.exists()).thenReturn(true);
65-
mockStatic(FileUtils.class);
66-
when(FileUtils.readFileToString(file)).thenReturn(content);
67-
when(file.getAbsolutePath()).thenReturn(absolutePath);
68-
when(file.lastModified()).thenReturn(lastModified);
64+
mockFile(true,"content","absolutePath");
6965

7066
lessSource = new LessSource(file);
7167

72-
assertEquals(absolutePath, lessSource.getAbsolutePath());
73-
assertEquals(content, lessSource.getContent());
74-
assertEquals(content, lessSource.getNormalizedContent());
68+
assertEquals("absolutePath", lessSource.getAbsolutePath());
69+
assertEquals("content", lessSource.getContent());
70+
assertEquals("content", lessSource.getNormalizedContent());
7571
assertEquals(lastModified, lessSource.getLastModified());
7672
assertEquals(lastModified, lessSource.getLastModifiedIncludingImports());
7773
assertEquals(0, lessSource.getImports().size());
@@ -93,11 +89,7 @@ public void testNewLessSourceFileNotFound() throws Exception {
9389

9490
@Test
9591
public void testLastModifiedIncludingImportsWhenNoImportModifiedLater() throws Exception {
96-
when(file.exists()).thenReturn(true);
97-
mockStatic(FileUtils.class);
98-
when(FileUtils.readFileToString(file)).thenReturn(content);
99-
when(file.getAbsolutePath()).thenReturn(absolutePath);
100-
when(file.lastModified()).thenReturn(1l);
92+
mockFile(true,"content","absolutePath");
10193

10294
when(import1.getLastModifiedIncludingImports()).thenReturn(0l);
10395
when(import2.getLastModifiedIncludingImports()).thenReturn(0l);
@@ -111,11 +103,7 @@ public void testLastModifiedIncludingImportsWhenNoImportModifiedLater() throws E
111103

112104
@Test
113105
public void testLastModifiedIncludingImportsWhenImportModifiedLater() throws Exception {
114-
when(file.exists()).thenReturn(true);
115-
mockStatic(FileUtils.class);
116-
when(FileUtils.readFileToString(file)).thenReturn(content);
117-
when(file.getAbsolutePath()).thenReturn(absolutePath);
118-
when(file.lastModified()).thenReturn(1l);
106+
mockFile(true,"content","absolutePath");
119107

120108
when(import1.getLastModifiedIncludingImports()).thenReturn(0l);
121109
when(import2.getLastModifiedIncludingImports()).thenReturn(2l);
@@ -126,4 +114,14 @@ public void testLastModifiedIncludingImportsWhenImportModifiedLater() throws Exc
126114

127115
assertEquals(2l, lessSource.getLastModifiedIncludingImports());
128116
}
117+
118+
private File mockFile(boolean fileExists, String content, String absolutePath) throws IOException {
119+
when(file.exists()).thenReturn(fileExists);
120+
mockStatic(FileUtils.class);
121+
when(FileUtils.readFileToString(file)).thenReturn(content);
122+
when(file.getAbsolutePath()).thenReturn(absolutePath);
123+
when(file.lastModified()).thenReturn(lastModified);
124+
when(file.getParent()).thenReturn("folder");
125+
return file;
126+
}
129127
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import 'import1.less';
2+
//@import 'import2.less';
3+
// @import 'import3.less';
4+
@import "import4";

0 commit comments

Comments
 (0)