Skip to content

Commit a126628

Browse files
authored
fix: run TaskGenerateHilla after npm install (#14339)
Fixes #14281
1 parent 020ee67 commit a126628

5 files changed

Lines changed: 237 additions & 48 deletions

File tree

‎flow-plugins/flow-plugin-base/src/test/java/com/vaadin/flow/plugin/base/BuildFrontendUtilTest.java‎

Lines changed: 146 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,90 @@
44
import java.io.FileOutputStream;
55
import java.io.IOException;
66
import java.io.PrintWriter;
7+
import java.net.URI;
78
import java.net.URISyntaxException;
89
import java.nio.charset.StandardCharsets;
910
import java.util.HashMap;
1011
import java.util.Map;
1112
import java.util.concurrent.TimeoutException;
1213

14+
import com.vaadin.experimental.FeatureFlags;
15+
import com.vaadin.flow.di.Lookup;
16+
import com.vaadin.flow.server.Constants;
17+
import com.vaadin.flow.server.ExecutionFailedException;
18+
import com.vaadin.flow.server.frontend.*;
19+
import com.vaadin.flow.server.frontend.installer.NodeInstaller;
20+
import org.apache.commons.io.FileUtils;
1321
import org.apache.commons.io.IOUtils;
1422
import org.junit.Assert;
1523
import org.junit.Assume;
24+
import org.junit.Before;
1625
import org.junit.Test;
1726
import org.junit.rules.TemporaryFolder;
27+
import org.mockito.InOrder;
28+
import org.mockito.MockedConstruction;
1829
import org.mockito.Mockito;
1930

20-
import com.vaadin.flow.server.frontend.FrontendTools;
21-
import com.vaadin.flow.server.frontend.FrontendUtils;
2231
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
2332
import com.vaadin.flow.utils.LookupImpl;
2433

2534
public class BuildFrontendUtilTest {
2635

27-
@Test
28-
public void testWebpackRequiredFlagsPassedToNodeEnvironment()
29-
throws IOException, URISyntaxException, TimeoutException {
30-
Assume.assumeFalse("Test not runnable on Windows",
31-
FrontendUtils.isWindows());
32-
Assume.assumeTrue("Test requires /bin/bash",
33-
new File("/bin/bash").exists());
36+
private File baseDir;
37+
38+
private PluginAdapterBuild adapter;
39+
40+
private FrontendTools tools;
3441

42+
private Lookup lookup;
43+
44+
@Before
45+
public void setup() throws IOException {
3546
TemporaryFolder tmpDir = new TemporaryFolder();
3647
tmpDir.create();
37-
File baseDir = tmpDir.newFolder();
48+
baseDir = tmpDir.newFolder();
49+
50+
adapter = Mockito.mock(PluginAdapterBuild.class);
51+
Mockito.when(adapter.npmFolder()).thenReturn(baseDir);
52+
Mockito.when(adapter.projectBaseDirectory())
53+
.thenReturn(tmpDir.getRoot().toPath());
54+
ClassFinder classFinder = Mockito.mock(ClassFinder.class);
55+
lookup = Mockito.spy(new LookupImpl(classFinder));
56+
Mockito.when(adapter.createLookup(Mockito.any())).thenReturn(lookup);
57+
Mockito.doReturn(classFinder).when(lookup).lookup(ClassFinder.class);
58+
59+
tools = Mockito.mock(FrontendTools.class);
3860

3961
// setup: mock a webpack executable
4062
File webpackBin = new File(baseDir, "node_modules/webpack/bin");
4163
Assert.assertTrue(webpackBin.mkdirs());
4264
File webPackExecutableMock = new File(webpackBin, "webpack.js");
4365
Assert.assertTrue(webPackExecutableMock.createNewFile());
4466

45-
PluginAdapterBase adapter = Mockito.mock(PluginAdapterBase.class);
46-
Mockito.when(adapter.npmFolder()).thenReturn(baseDir);
47-
Mockito.when(adapter.projectBaseDirectory())
48-
.thenReturn(tmpDir.getRoot().toPath());
49-
ClassFinder classFinder = Mockito.mock(ClassFinder.class);
50-
Mockito.when(adapter.createLookup(Mockito.any()))
51-
.thenReturn(new LookupImpl(classFinder));
67+
// setup: mock a vite executable
68+
File viteBin = new File(baseDir, "node_modules/vite/bin");
69+
Assert.assertTrue(viteBin.mkdirs());
70+
File viteExecutableMock = new File(viteBin, "vite.js");
71+
Assert.assertTrue(viteExecutableMock.createNewFile());
5272

53-
FrontendTools tools = Mockito.mock(FrontendTools.class);
73+
File resourceOutput = new File(baseDir, "resOut");
74+
Mockito.when(adapter.servletResourceOutputDirectory())
75+
.thenReturn(resourceOutput);
76+
File statsJson = new File(new File(resourceOutput, "config"),
77+
"stats.json");
78+
statsJson.getParentFile().mkdirs();
79+
try (FileOutputStream out = new FileOutputStream(statsJson)) {
80+
IOUtils.write("{\"npmModules\":[]}", out, StandardCharsets.UTF_8);
81+
}
82+
}
83+
84+
@Test
85+
public void testWebpackRequiredFlagsPassedToNodeEnvironment()
86+
throws IOException, URISyntaxException, TimeoutException {
87+
Assume.assumeFalse("Test not runnable on Windows",
88+
FrontendUtils.isWindows());
89+
Assume.assumeTrue("Test requires /bin/bash",
90+
new File("/bin/bash").exists());
5491

5592
// given: "node" stub that exits normally only if expected environment
5693
// set
@@ -64,22 +101,104 @@ public void testWebpackRequiredFlagsPassedToNodeEnvironment()
64101
Mockito.when(tools.getNodeExecutable())
65102
.thenReturn(fakeNode.getAbsolutePath());
66103

67-
File resourceOutput = new File(baseDir, "resOut");
68-
Mockito.when(adapter.servletResourceOutputDirectory())
69-
.thenReturn(resourceOutput);
70104
Map<String, String> environment = new HashMap<>();
71105
environment.put("NODE_OPTIONS", "expected");
72106
Mockito.when(tools.getWebpackNodeEnvironment()).thenReturn(environment);
73107

74-
File statsJson = new File(new File(resourceOutput, "config"),
75-
"stats.json");
76-
statsJson.getParentFile().mkdirs();
77-
try (FileOutputStream out = new FileOutputStream(statsJson)) {
78-
IOUtils.write("{\"npmModules\":[]}", out, StandardCharsets.UTF_8);
79-
}
80108
// then
81109
BuildFrontendUtil.runWebpack(adapter, tools);
82110

83111
// terminates successfully
84112
}
113+
114+
@Test
115+
public void should_notUseHilla_inPrepareFrontend()
116+
throws ExecutionFailedException, IOException, URISyntaxException {
117+
setupPluginAdapterDefaults();
118+
119+
File openApiJsonFile = new File(new File(baseDir, Constants.TARGET),
120+
FrontendUtils.DEFAULT_CONNECT_OPENAPI_JSON_FILE);
121+
Mockito.when(adapter.openApiJsonFile()).thenReturn(openApiJsonFile);
122+
123+
BuildFrontendUtil.prepareFrontend(adapter);
124+
125+
Mockito.verify(lookup, Mockito.never())
126+
.lookup(EndpointGeneratorTaskFactory.class);
127+
Mockito.verify(lookup, Mockito.never()).lookup(TaskGenerateHilla.class);
128+
Mockito.verify(adapter, Mockito.never()).openApiJsonFile();
129+
}
130+
131+
@Test
132+
public void should_useOldEndpointGenerator_withNodeUpdater()
133+
throws URISyntaxException, ExecutionFailedException {
134+
setupPluginAdapterDefaults();
135+
136+
BuildFrontendUtil.runNodeUpdater(adapter);
137+
138+
Mockito.verify(lookup).lookup(EndpointGeneratorTaskFactory.class);
139+
Mockito.verify(lookup, Mockito.never()).lookup(TaskGenerateHilla.class);
140+
}
141+
142+
@Test
143+
public void should_useHillaEngine_withNodeUpdater()
144+
throws URISyntaxException, ExecutionFailedException, IOException {
145+
setupPluginAdapterDefaults();
146+
147+
MockedConstruction<TaskRunNpmInstall> construction = Mockito
148+
.mockConstruction(TaskRunNpmInstall.class);
149+
150+
final TaskGenerateHilla taskGenerateHilla = Mockito
151+
.mock(TaskGenerateHilla.class);
152+
Mockito.doReturn(taskGenerateHilla).when(lookup)
153+
.lookup(TaskGenerateHilla.class);
154+
155+
FileUtils.write(
156+
new File(adapter.javaResourceFolder(),
157+
FeatureFlags.PROPERTIES_FILENAME),
158+
"com.vaadin.experimental.hillaEngine=true\n",
159+
StandardCharsets.UTF_8);
160+
161+
BuildFrontendUtil.runNodeUpdater(adapter);
162+
163+
Mockito.verify(lookup, Mockito.never())
164+
.lookup(EndpointGeneratorTaskFactory.class);
165+
Mockito.verify(lookup).lookup(TaskGenerateHilla.class);
166+
Mockito.verify(taskGenerateHilla).configure(adapter.npmFolder(),
167+
adapter.buildFolder());
168+
169+
// Hilla Engine requires npm install, the order of execution is critical
170+
final TaskRunNpmInstall taskRunNpmInstall = construction.constructed()
171+
.get(0);
172+
InOrder inOrder = Mockito.inOrder(taskRunNpmInstall, taskGenerateHilla);
173+
inOrder.verify(taskRunNpmInstall).execute();
174+
inOrder.verify(taskGenerateHilla).execute();
175+
}
176+
177+
private void setupPluginAdapterDefaults() throws URISyntaxException {
178+
Mockito.when(adapter.nodeVersion())
179+
.thenReturn(FrontendTools.DEFAULT_NODE_VERSION);
180+
Mockito.when(adapter.nodeDownloadRoot()).thenReturn(
181+
URI.create(NodeInstaller.DEFAULT_NODEJS_DOWNLOAD_ROOT));
182+
Mockito.when(adapter.frontendDirectory()).thenReturn(
183+
new File(baseDir, FrontendUtils.DEFAULT_FRONTEND_DIR));
184+
Mockito.when(adapter.generatedFolder()).thenReturn(new File(baseDir,
185+
FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR));
186+
Mockito.when(adapter.buildFolder()).thenReturn(Constants.TARGET);
187+
Mockito.when(adapter.npmFolder()).thenReturn(baseDir);
188+
File javaSourceFolder = new File(baseDir,
189+
FrontendUtils.DEFAULT_CONNECT_JAVA_SOURCE_FOLDER);
190+
Assert.assertTrue(javaSourceFolder.mkdirs());
191+
Mockito.when(adapter.javaSourceFolder()).thenReturn(javaSourceFolder);
192+
File javaResourceFolder = new File(baseDir, "src/main/resources");
193+
Assert.assertTrue(javaResourceFolder.mkdirs());
194+
Mockito.when(adapter.javaResourceFolder())
195+
.thenReturn(javaResourceFolder);
196+
Mockito.when(adapter.openApiJsonFile())
197+
.thenReturn(new File(new File(baseDir, Constants.TARGET),
198+
FrontendUtils.DEFAULT_CONNECT_OPENAPI_JSON_FILE));
199+
Mockito.when(adapter.getClassFinder())
200+
.thenReturn(new ClassFinder.DefaultClassFinder(
201+
this.getClass().getClassLoader()));
202+
Mockito.when(adapter.runNpmInstall()).thenReturn(true);
203+
}
85204
}

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/NodeTasks.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,6 @@ public File getFlowResourcesFolder() {
696696
TaskGenerateTsConfig.class,
697697
TaskGenerateTsDefinitions.class,
698698
TaskGenerateServiceWorker.class,
699-
TaskGenerateHilla.class,
700699
TaskGenerateOpenAPI.class,
701700
TaskGenerateEndpoint.class,
702701
TaskGenerateBootstrap.class,
@@ -706,6 +705,7 @@ public File getFlowResourcesFolder() {
706705
TaskInstallWebpackPlugins.class,
707706
TaskUpdatePackages.class,
708707
TaskRunNpmInstall.class,
708+
TaskGenerateHilla.class,
709709
TaskCopyFrontendFiles.class,
710710
TaskCopyLocalFrontendFiles.class,
711711
TaskUpdateSettingsFile.class,
@@ -793,18 +793,18 @@ private NodeTasks(Builder builder) {
793793

794794
// use the new Hilla generator if enabled, otherwise use the old
795795
// generator.
796-
if (featureFlags.isEnabled(FeatureFlags.HILLA_ENGINE)) {
797-
TaskGenerateHilla hillaTask = builder.lookup
798-
.lookup(TaskGenerateHilla.class);
796+
TaskGenerateHilla hillaTask;
797+
if (builder.endpointGeneratedOpenAPIFile != null
798+
&& featureFlags.isEnabled(FeatureFlags.HILLA_ENGINE)
799+
&& (hillaTask = builder.lookup
800+
.lookup(TaskGenerateHilla.class)) != null) {
799801
hillaTask.configure(builder.getNpmFolder(),
800802
builder.getBuildDirectory());
801803
commands.add(hillaTask);
802-
} else {
803-
if (builder.endpointSourceFolder != null
804-
&& builder.endpointSourceFolder.exists()
805-
&& builder.endpointGeneratedOpenAPIFile != null) {
806-
addEndpointServicesTasks(builder);
807-
}
804+
} else if (builder.endpointGeneratedOpenAPIFile != null
805+
&& builder.endpointSourceFolder != null
806+
&& builder.endpointSourceFolder.exists()) {
807+
addEndpointServicesTasks(builder);
808808
}
809809

810810
commands.add(new TaskGenerateBootstrap(frontendDependencies,

‎flow-server/src/test/java/com/vaadin/flow/server/frontend/NodeTasksHillaTest.java‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class NodeTasksHillaTest {
7070
@Mock
7171
private TaskGenerateHilla taskGenerateHilla;
7272

73+
private Builder builder;
74+
7375
@Before
7476
public void setup() throws Exception {
7577
userDir = temporaryFolder.getRoot().getAbsolutePath();
@@ -92,6 +94,16 @@ public void setup() throws Exception {
9294

9395
Mockito.doReturn(taskGenerateHilla).when(lookup)
9496
.lookup(TaskGenerateHilla.class);
97+
98+
builder = new Builder(lookup, new File(userDir), TARGET)
99+
.enablePackagesUpdate(false).enableImportsUpdate(true)
100+
.runNpmInstall(false).withEmbeddableWebComponents(false)
101+
.withFlowResourcesFolder(
102+
new File(userDir, TARGET + "flow-frontend"))
103+
.withFrontendGeneratedFolder(new File(userDir))
104+
.withEndpointSourceFolder(new File(userDir))
105+
.withEndpointGeneratedOpenAPIFile(new File(userDir))
106+
.setJavaResourceFolder(propertiesDir);
95107
}
96108

97109
@BeforeClass
@@ -109,7 +121,7 @@ public static void tearDownAfterClass() {
109121
}
110122

111123
@Test
112-
public void should_useHillaGeneartor_whenEnabled()
124+
public void should_useHillaEngine_whenEnabled()
113125
throws ExecutionFailedException, IOException {
114126
runEndpointTasks(true);
115127
verifyHillaTask(true);
@@ -124,6 +136,26 @@ public void should_useOldGenerator_whenHillaGeneratorNotEnabled()
124136
verifyOldGenerator(true);
125137
}
126138

139+
@Test
140+
public void should_notHillaEngine_whenOpenAPIFileIsNull()
141+
throws ExecutionFailedException, IOException {
142+
builder.withEndpointGeneratedOpenAPIFile(null);
143+
144+
runEndpointTasks(true);
145+
verifyHillaTask(false);
146+
verifyOldGenerator(false);
147+
}
148+
149+
@Test
150+
public void should_notUseOldGenerator_whenOpenAPIFileIsNull()
151+
throws ExecutionFailedException, IOException {
152+
builder.withEndpointGeneratedOpenAPIFile(null);
153+
154+
runEndpointTasks(false);
155+
verifyHillaTask(false);
156+
verifyOldGenerator(false);
157+
}
158+
127159
private void runEndpointTasks(boolean withHillaTask)
128160
throws ExecutionFailedException, IOException {
129161
FileUtils.write(
@@ -132,16 +164,6 @@ private void runEndpointTasks(boolean withHillaTask)
132164
withHillaTask),
133165
StandardCharsets.UTF_8);
134166

135-
Builder builder = new Builder(lookup, new File(userDir), TARGET)
136-
.enablePackagesUpdate(false).enableImportsUpdate(true)
137-
.runNpmInstall(false).withEmbeddableWebComponents(false)
138-
.withFlowResourcesFolder(
139-
new File(userDir, TARGET + "flow-frontend"))
140-
.withFrontendGeneratedFolder(new File(userDir))
141-
.withEndpointSourceFolder(new File(userDir))
142-
.withEndpointGeneratedOpenAPIFile(new File(userDir))
143-
.setJavaResourceFolder(propertiesDir);
144-
145167
builder.build().execute();
146168
}
147169

0 commit comments

Comments
 (0)