Skip to content

Commit 433c46c

Browse files
authored
fix: copy package-lock from bundle in hotdeploy (#17128)
* fix: copy package-lock from bundle in hotdeploy Copy the package-lock.json from the dev bundle in frontend hotdeploy mode if no package-lock is available. * add test for copy in frontend mode Fix tests to put needs_build file to temp directory
1 parent fd330df commit 433c46c

2 files changed

Lines changed: 57 additions & 18 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public NodeTasks(Options options) {
149149
UsageStatistics.markAsUsed("flow/dev-bundle", null);
150150
}
151151
}
152+
} else if (options.isFrontendHotdeploy()) {
153+
BundleUtils.copyPackageLockFromBundle(options);
152154
}
153155

154156
if (options.isGenerateEmbeddableWebComponents()) {

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

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@
2828
import org.junit.Rule;
2929
import org.junit.Test;
3030
import org.junit.rules.TemporaryFolder;
31+
import org.mockito.MockedStatic;
3132
import org.mockito.Mockito;
3233

3334
import com.vaadin.experimental.FeatureFlags;
3435
import com.vaadin.flow.component.dependency.JavaScript;
3536
import com.vaadin.flow.component.dependency.JsModule;
3637
import com.vaadin.flow.di.Lookup;
38+
import com.vaadin.flow.server.Constants;
3739
import com.vaadin.flow.server.ExecutionFailedException;
40+
import com.vaadin.flow.server.Mode;
3841
import com.vaadin.flow.server.frontend.NodeTestComponents.ExampleExperimentalComponent;
3942
import com.vaadin.flow.server.frontend.NodeTestComponents.FlagView;
4043
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
4144
import com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder;
45+
import com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner;
4246

4347
import static com.vaadin.flow.server.Constants.TARGET;
4448
import static com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR;
@@ -47,6 +51,7 @@
4751
import static org.junit.Assert.assertEquals;
4852
import static org.junit.Assert.assertFalse;
4953
import static org.junit.Assert.assertTrue;
54+
import static org.mockito.ArgumentMatchers.any;
5055
import static org.mockito.Mockito.mock;
5156

5257
public class NodeTasksViteTest {
@@ -60,10 +65,12 @@ public class NodeTasksViteTest {
6065
private static String globalFrontendDirValue;
6166

6267
private String userDir;
68+
private File npmFolder;
6369

6470
@Before
6571
public void setup() {
6672
userDir = temporaryFolder.getRoot().getAbsolutePath();
73+
npmFolder = new File(userDir);
6774
System.setProperty(USER_DIR, userDir);
6875
System.clearProperty(PARAM_FRONTEND_DIR);
6976
}
@@ -90,12 +97,12 @@ public void should_ExcludeExperimentalComponent_WhenFeatureDisabled()
9097
ClassFinder finder = NodeUpdateTestUtil.getClassFinder(classes);
9198
Mockito.doReturn(finder).when(mockedLookup).lookup(ClassFinder.class);
9299

93-
Options options = new Options(mockedLookup, new File(userDir))
100+
Options options = new Options(mockedLookup, npmFolder)
94101
.withBuildDirectory(TARGET).enablePackagesUpdate(false)
95102
.enableImportsUpdate(true).withRunNpmInstall(false)
96103
.withEmbeddableWebComponents(false)
97-
.withJarFrontendResourcesFolder(
98-
getJarFrontendResourcesFolder());
104+
.withJarFrontendResourcesFolder(getJarFrontendResourcesFolder())
105+
.withWebpack(npmFolder, npmFolder);
99106

100107
assertEquals(1, finder.getAnnotatedClasses(JsModule.class).size());
101108
assertEquals(1, finder.getAnnotatedClasses(JavaScript.class).size());
@@ -129,13 +136,13 @@ public void should_IncludeExperimentalComponent_WhenFeatureEnabled()
129136
ClassFinder finder = NodeUpdateTestUtil.getClassFinder(classes);
130137
Mockito.doReturn(finder).when(mockedLookup).lookup(ClassFinder.class);
131138

132-
Options options = new Options(mockedLookup, new File(userDir))
139+
Options options = new Options(mockedLookup, npmFolder)
133140
.withBuildDirectory(TARGET).enablePackagesUpdate(false)
134141
.enableImportsUpdate(true).withRunNpmInstall(false)
135142
.withEmbeddableWebComponents(false)
136143
.setJavaResourceFolder(propertiesDir)
137-
.withJarFrontendResourcesFolder(
138-
getJarFrontendResourcesFolder());
144+
.withJarFrontendResourcesFolder(getJarFrontendResourcesFolder())
145+
.withWebpack(npmFolder, npmFolder);
139146

140147
new NodeTasks(options).execute();
141148
File importsFile = FrontendUtils
@@ -156,12 +163,12 @@ public void should_UseDefaultFolders() throws Exception {
156163
Mockito.doReturn(
157164
new DefaultClassFinder(this.getClass().getClassLoader()))
158165
.when(mockedLookup).lookup(ClassFinder.class);
159-
Options options = new Options(mockedLookup, new File(userDir))
166+
Options options = new Options(mockedLookup, npmFolder)
160167
.withBuildDirectory(TARGET).enablePackagesUpdate(false)
161168
.enableImportsUpdate(true).withRunNpmInstall(false)
162169
.withEmbeddableWebComponents(false)
163-
.withJarFrontendResourcesFolder(
164-
getJarFrontendResourcesFolder());
170+
.withJarFrontendResourcesFolder(getJarFrontendResourcesFolder())
171+
.withWebpack(npmFolder, npmFolder);
165172

166173
Assert.assertEquals(
167174
new File(userDir, DEFAULT_FRONTEND_DIR).getAbsolutePath(),
@@ -179,12 +186,12 @@ public void should_generateServiceWorkerWhenPwa() throws Exception {
179186
Mockito.doReturn(
180187
new DefaultClassFinder(this.getClass().getClassLoader()))
181188
.when(mockedLookup).lookup(ClassFinder.class);
182-
Options options = new Options(mockedLookup, new File(userDir))
189+
Options options = new Options(mockedLookup, npmFolder)
183190
.withBuildDirectory(TARGET).enablePackagesUpdate(false)
184191
.enableImportsUpdate(true).withRunNpmInstall(false)
185192
.withEmbeddableWebComponents(false)
186-
.withJarFrontendResourcesFolder(
187-
getJarFrontendResourcesFolder());
193+
.withJarFrontendResourcesFolder(getJarFrontendResourcesFolder())
194+
.withWebpack(npmFolder, npmFolder);
188195

189196
Assert.assertEquals(
190197
new File(userDir, DEFAULT_FRONTEND_DIR).getAbsolutePath(),
@@ -203,12 +210,12 @@ public void should_BeAbleToCustomizeFolders() throws Exception {
203210
Mockito.doReturn(
204211
new DefaultClassFinder(this.getClass().getClassLoader()))
205212
.when(mockedLookup).lookup(ClassFinder.class);
206-
Options options = new Options(mockedLookup, new File(userDir))
213+
Options options = new Options(mockedLookup, npmFolder)
207214
.withBuildDirectory(TARGET).enablePackagesUpdate(false)
208215
.enableImportsUpdate(true).withRunNpmInstall(false)
209216
.withEmbeddableWebComponents(false)
210-
.withJarFrontendResourcesFolder(
211-
getJarFrontendResourcesFolder());
217+
.withJarFrontendResourcesFolder(getJarFrontendResourcesFolder())
218+
.withWebpack(npmFolder, npmFolder);
212219

213220
Assert.assertEquals(
214221
new File(userDir, "my_custom_sources_folder").getAbsolutePath(),
@@ -235,18 +242,48 @@ public void should_GenerateTsConfigAndTsDefinitions_When_Vaadin14BootstrapMode()
235242
Mockito.doReturn(
236243
new DefaultClassFinder(this.getClass().getClassLoader()))
237244
.when(mockedLookup).lookup(ClassFinder.class);
238-
Options options = new Options(mockedLookup, new File(userDir))
245+
Options options = new Options(mockedLookup, npmFolder)
239246
.withBuildDirectory(TARGET).enablePackagesUpdate(false)
240247
.enableImportsUpdate(true).withRunNpmInstall(false)
241248
.withEmbeddableWebComponents(false)
242-
.withJarFrontendResourcesFolder(
243-
getJarFrontendResourcesFolder());
249+
.withJarFrontendResourcesFolder(getJarFrontendResourcesFolder())
250+
.withWebpack(npmFolder, npmFolder);
244251
new NodeTasks(options).execute();
245252

246253
Assert.assertTrue(new File(userDir, "tsconfig.json").exists());
247254
Assert.assertTrue(new File(userDir, "types.d.ts").exists());
248255
}
249256

257+
@Test
258+
public void should_copyPackageLockJson_When_frontendHotdeploy()
259+
throws ExecutionFailedException {
260+
Lookup mockedLookup = mock(Lookup.class);
261+
Mockito.doReturn(
262+
new DefaultClassFinder(this.getClass().getClassLoader()))
263+
.when(mockedLookup).lookup(ClassFinder.class);
264+
Options options = new Options(mockedLookup, npmFolder)
265+
.withBuildDirectory(TARGET).enablePackagesUpdate(false)
266+
.enableImportsUpdate(true).withRunNpmInstall(false)
267+
.withEmbeddableWebComponents(false)
268+
.withJarFrontendResourcesFolder(getJarFrontendResourcesFolder())
269+
.withFrontendHotdeploy(true).withProductionMode(false)
270+
.withWebpack(npmFolder, npmFolder);
271+
try (MockedStatic<BundleUtils> bundleUtils = Mockito
272+
.mockStatic(BundleUtils.class);
273+
MockedStatic<BundleValidationUtil> validationUtil = Mockito
274+
.mockStatic(BundleValidationUtil.class)) {
275+
new NodeTasks(options).execute();
276+
bundleUtils.verify(
277+
() -> BundleUtils.copyPackageLockFromBundle(options),
278+
Mockito.times(1));
279+
validationUtil.verify(
280+
() -> BundleValidationUtil.needsBuild(any(Options.class),
281+
any(FrontendDependenciesScanner.class),
282+
any(ClassFinder.class), any(Mode.class)),
283+
Mockito.never());
284+
}
285+
}
286+
250287
private static void setPropertyIfPresent(String key, String value) {
251288
if (value != null) {
252289
System.setProperty(key, value);

0 commit comments

Comments
 (0)