-
Notifications
You must be signed in to change notification settings - Fork 160
/
AbstractNodeUpdateImportsTest.java
302 lines (250 loc) · 11.4 KB
/
AbstractNodeUpdateImportsTest.java
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.vaadin.flow.server.frontend;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.slf4j.Logger;
import com.vaadin.experimental.FeatureFlags;
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
import com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner;
import static com.vaadin.flow.server.Constants.TARGET;
import static com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR;
import static com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR;
import static com.vaadin.flow.server.frontend.FrontendUtils.FLOW_NPM_PACKAGE_NAME;
import static com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME;
import static com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public abstract class AbstractNodeUpdateImportsTest extends NodeUpdateTestUtil {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public ExpectedException exception = ExpectedException.none();
private File importsFile;
private File generatedPath;
private File frontendDirectory;
private File nodeModulesPath;
private TaskUpdateImports updater;
private MockLogger logger;
@Before
public void setup() throws Exception {
File tmpRoot = temporaryFolder.getRoot();
logger = new MockLogger();
frontendDirectory = new File(tmpRoot, DEFAULT_FRONTEND_DIR);
nodeModulesPath = new File(tmpRoot, NODE_MODULES);
generatedPath = new File(tmpRoot,
Paths.get(TARGET, DEFAULT_GENERATED_DIR).toString());
importsFile = new File(generatedPath, IMPORTS_NAME);
ClassFinder classFinder = getClassFinder();
updater = new TaskUpdateImports(classFinder, getScanner(classFinder),
finder -> null, tmpRoot, generatedPath, frontendDirectory, null,
null, false, TARGET, true, false,
Mockito.mock(FeatureFlags.class)) {
@Override
Logger log() {
return logger;
}
};
assertTrue(nodeModulesPath.mkdirs());
createExpectedImports(frontendDirectory, nodeModulesPath);
assertTrue(new File(nodeModulesPath,
FLOW_NPM_PACKAGE_NAME + "ExampleConnector.js").exists());
}
protected abstract FrontendDependenciesScanner getScanner(
ClassFinder finder);
@Test
public void generateImportsFile_fileContainsThemeLinesAndExpectedImportsAndCssImportLinesAndLogReports()
throws Exception {
List<String> expectedLines = new ArrayList<>(Arrays.asList(
"export const addCssBlock = function(block, before = false) {",
" const tpl = document.createElement('template');",
" tpl.innerHTML = block;",
" document.head[before ? 'insertBefore' : 'appendChild'](tpl.content, document.head.firstChild);",
"};",
"addCssBlock('<custom-style><style include=\"lumo-color lumo-typography\"></style></custom-style>', true);",
"document.documentElement.setAttribute('theme', 'dark');"));
expectedLines.addAll(getExpectedImports());
// An import without `.js` extension
expectedLines.add(
"import '@vaadin/vaadin-mixed-component/theme/lumo/vaadin-something-else'");
// An import not found in node_modules
expectedLines.add("import 'unresolved/component';");
expectedLines.add(
"import $cssFromFile_0 from '@vaadin/vaadin-mixed-component/bar.css'");
expectedLines.add("import $cssFromFile_1 from 'Frontend/foo.css';");
expectedLines.add("import $cssFromFile_2 from 'Frontend/foo.css';");
expectedLines.add("import $cssFromFile_3 from 'Frontend/foo.css';");
expectedLines.add("import $cssFromFile_4 from 'Frontend/foo.css';");
expectedLines.add("import $cssFromFile_5 from 'Frontend/foo.css';");
expectedLines.add("import $cssFromFile_6 from 'Frontend/foo.css';");
expectedLines.add(
"import { css, unsafeCSS, registerStyles } from '@vaadin/vaadin-themable-mixin';");
expectedLines.add("addCssBlock(`<style>${$css_0}</style>`);");
expectedLines.add("addCssBlock(`<style>${$css_1}</style>`);");
expectedLines.add(
"addCssBlock(`<style include=\"bar\">${$css_2}</style>`);");
expectedLines.add("registerStyles('', $css_3, {moduleId: 'baz'});");
expectedLines.add(
"registerStyles('', $css_4, {include: 'bar', moduleId: 'baz'});");
expectedLines.add(
"registerStyles('foo-bar', $css_5, {moduleId: 'flow_css_mod'});");
expectedLines.add(
"registerStyles('foo-bar', $css_6, {include: 'bar', moduleId: 'flow_css_mod'});");
assertFalse(importsFile.exists());
updater.execute();
assertTrue(importsFile.exists());
assertContainsImports(true, expectedLines.toArray(new String[0]));
String output = logger.getLogs();
assertContains(output, true,
"Use the './' prefix for files in JAR files: 'ExampleConnector.js'",
"Use the './' prefix for files in the '"
+ frontendDirectory.getPath()
+ "' folder: 'vaadin-mixed-component/theme/lumo/vaadin-mixed-component.js'");
// Using regex match because of the ➜ character in TC
assertContains(output, true,
"Failed to find the following imports in the `node_modules` tree:\n - unresolved/component");
}
@Test
public void noChanges_generatedJsFileIsNotUpdated() throws Exception {
updater.execute();
long timestamp1 = importsFile.lastModified();
// need to sleep because timestamp is in seconds
sleep(1000);
updater.execute();
long timestamp2 = importsFile.lastModified();
Assert.assertEquals(timestamp1, timestamp2);
}
@Test
public void removeJsModuleImportFromFile_importIsReadedAfterRegeneration()
throws Exception {
updater.execute();
removeImports("@vaadin/vaadin-lumo-styles/sizing.js",
"./local-template.js");
assertContainsImports(false, "@vaadin/vaadin-lumo-styles/sizing.js",
"./local-template.js");
updater.execute();
assertContainsImports(true, "@vaadin/vaadin-lumo-styles/sizing.js",
"./local-template.js");
}
@Test
public void addModuleImportManuallyIntoGeneratedFile_importIsRemovedAfterRegeneration()
throws Exception {
updater.execute();
addImports("./added-import.js");
assertContainsImports(true, "./added-import.js");
updater.execute();
assertContainsImports(false, "./added-import.js");
}
@Test
public void addAndRemoveJsModuleImports_addedImportIsNotPreseredAfterRegeneration()
throws Exception {
updater.execute();
removeImports("@vaadin/vaadin-lumo-styles/sizing.js",
"./local-template.js");
addImports("./added-import.js");
assertContainsImports(false, "@vaadin/vaadin-lumo-styles/sizing.js",
"./local-template.js");
assertContainsImports(true, "./added-import.js");
updater.execute();
assertContainsImports(false, "./added-import.js");
}
@Test
public void addJsModules_themeModulesAreOnTop() throws Exception {
updater.execute();
addImports("styles/styles.js");
assertImportOrder("@vaadin/vaadin-lumo-styles/color.js",
"Frontend/foo.js");
assertImportOrder("@vaadin/vaadin-lumo-styles/color.js",
"styles/styles.js");
}
private void assertContainsImports(boolean contains, String... imports)
throws IOException {
String content = FileUtils.readFileToString(importsFile,
Charset.defaultCharset());
for (String line : imports) {
assertContains(content, contains, addWebpackPrefix(line));
}
}
private void assertContains(String content, boolean contains,
String... checks) {
for (String importString : checks) {
boolean result = content.contains(importString);
String message = "\n " + (contains ? "NOT " : "") + "FOUND '"
+ importString + " IN: \n" + content;
if (contains) {
assertTrue(message, result);
} else {
assertFalse(message, result);
}
}
}
private void assertImportOrder(String... imports) throws IOException {
String content = FileUtils.readFileToString(importsFile,
Charset.defaultCharset());
int curIndex = -1;
for (String line : imports) {
String prefixed = addWebpackPrefix(line);
int nextIndex = content.indexOf(prefixed);
assertTrue("import '" + prefixed + "' not found", nextIndex != -1);
assertTrue("import '" + prefixed + "' appears in the wrong order",
curIndex <= nextIndex);
curIndex = nextIndex;
}
}
private void removeImports(String... imports) throws IOException {
List<String> importsList = Arrays.asList(imports);
List<String> current = FileUtils.readLines(importsFile,
Charset.defaultCharset());
Set<String> removed = current
.stream().filter(line -> importsList.stream()
.map(this::addWebpackPrefix).anyMatch(line::contains))
.collect(Collectors.toSet());
current.removeAll(removed);
String content = String.join("\n", current);
replaceJsFile(content + "\n");
}
private void addImports(String... imports) throws IOException {
String content = Arrays.stream(imports).map(this::addWebpackPrefix)
.map(s -> "import '" + s + "';")
.collect(Collectors.joining("\n"));
replaceJsFile(content + "\n", StandardOpenOption.APPEND);
}
private void replaceJsFile(String content, OpenOption... options)
throws IOException {
Files.write(Paths.get(importsFile.toURI()),
content.getBytes(StandardCharsets.UTF_8), options);
}
}