Skip to content

Commit

Permalink
fix: separate core and commercial components version files (#14093) (#…
Browse files Browse the repository at this point in the history
…14104)

* fix: separate core and commercial components version files (#14014)

* fix: separate core and commercial components version files

Fixes: #13749

Previously, the vaadin_versions.json contained
both core and commercial components listed
which led to including them all in package.json
even if the user only used vaadin-core. Now
platform generates vaadin-core-version.json in
vaadin-core and vaadin-version.json to include
commercial components in vaadin artifact.
This commit will adopt having separate files
while generating package.json and pinning the
platform dependencies.

(cherry picked from commit 23f5496)

* test: add tests for handling separate versions in NodeUpdater (#14079)

Tests are added for changes done in PR
#14014

(cherry picked from commit 7140d32)

* chore: update log message after versions.json separation (#14083)

(cherry picked from commit 1b86801)

Co-authored-by: Zhe Sun <[email protected]>

Co-authored-by: Soroosh Taefi <[email protected]>
  • Loading branch information
vaadin-bot and taefi authored Jul 1, 2022
1 parent e2f5d2f commit 7462cbe
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,14 @@ public final class Constants implements Serializable {
public static final String STATISTIC_ROUTING_HYBRID = "routing/hybrid";

/**
* The name of platform versions file.
* The name of platform core components and tools versions file.
*/
public static final String VAADIN_VERSIONS_JSON = "vaadin_versions.json";
public static final String VAADIN_CORE_VERSIONS_JSON = "vaadin-core-versions.json";

/**
* The name of platform commercial components and tools versions file.
*/
public static final String VAADIN_VERSIONS_JSON = "vaadin-versions.json";

/**
* @deprecated Use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Platform implements Serializable {
*/
public static Optional<String> getVaadinVersion() {
try (InputStream vaadinVersionsStream = Platform.class.getClassLoader()
.getResourceAsStream(Constants.VAADIN_VERSIONS_JSON)) {
.getResourceAsStream(Constants.VAADIN_CORE_VERSIONS_JSON)) {
if (vaadinVersionsStream != null) {
ObjectMapper m = new ObjectMapper();
JsonNode vaadinVersions = m.readTree(vaadinVersionsStream);
Expand All @@ -49,7 +49,7 @@ public static Optional<String> getVaadinVersion() {
versionErrorLogged = true;
LoggerFactory.getLogger(Platform.class)
.info("Unable to determine version information. "
+ "No vaadin_versions.json found");
+ "No vaadin-core-versions.json found");
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,45 @@ protected File getPackageLockFile() {
* when versions file could not be read
*/
JsonObject getPlatformPinnedDependencies() throws IOException {
URL resource = finder.getResource(Constants.VAADIN_VERSIONS_JSON);
if (resource == null) {
log().info("Couldn't find {} file to pin dependency versions."
+ " Transitive dependencies won't be pinned for pnpm.",
Constants.VAADIN_VERSIONS_JSON);
URL coreVersionsResource = finder
.getResource(Constants.VAADIN_CORE_VERSIONS_JSON);
if (coreVersionsResource == null) {
log().info(
"Couldn't find {} file to pin dependency versions for core components."
+ " Transitive dependencies won't be pinned for npm/pnpm.",
Constants.VAADIN_CORE_VERSIONS_JSON);
return Json.createObject();
}

JsonObject versionsJson = getFilteredVersionsFromResource(
coreVersionsResource, Constants.VAADIN_CORE_VERSIONS_JSON);

URL vaadinVersionsResource = finder
.getResource(Constants.VAADIN_VERSIONS_JSON);
if (vaadinVersionsResource == null) {
// vaadin is not on the classpath, only vaadin-core is present.
return versionsJson;
}

JsonObject vaadinVersionsJson = getFilteredVersionsFromResource(
vaadinVersionsResource, Constants.VAADIN_VERSIONS_JSON);
for (String key : vaadinVersionsJson.keys()) {
versionsJson.put(key, vaadinVersionsJson.getString(key));
}

return versionsJson;
}

private JsonObject getFilteredVersionsFromResource(URL versionsResource,
String versionsOrigin) throws IOException {
JsonObject versionsJson;
try (InputStream content = resource.openStream()) {
try (InputStream content = versionsResource.openStream()) {
VersionsJsonConverter convert = new VersionsJsonConverter(Json
.parse(IOUtils.toString(content, StandardCharsets.UTF_8)));
versionsJson = convert.getConvertedJson();
versionsJson = new VersionsJsonFilter(getPackageJson(),
DEPENDENCIES).getFilteredVersions(versionsJson);
DEPENDENCIES).getFilteredVersions(versionsJson,
versionsOrigin);
}
return versionsJson;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ class VersionsJsonFilter {
*
* @param versions
* to be filtered for user managed ones
* @param versionOrigin
* origin of the version (like a file), used in error message
* @return filtered versions json
*/
JsonObject getFilteredVersions(JsonObject versions) {
JsonObject getFilteredVersions(JsonObject versions, String versionOrigin) {
JsonObject json = Json.createObject();
for (String key : versions.keys()) {
final FrontendVersion version = FrontendUtils
.getPackageVersionFromJson(versions, key,
"vaadin_version.json");
.getPackageVersionFromJson(versions, key, versionOrigin);
if (version == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public void setup() throws Exception {
classFinder = Mockito.spy(getClassFinder());
File versions = temporaryFolder.newFile();
FileUtils.write(versions, "{}", StandardCharsets.UTF_8);
Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(
classFinder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versions.toURI().toURL());

packageUpdater = new TaskUpdatePackages(classFinder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public void setup() throws Exception {
StandardCharsets.UTF_8);
// @formatter:on

Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(
classFinder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versions.toURI().toURL());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.experimental.FeatureFlags;
import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
import com.vaadin.flow.server.frontend.scanner.FrontendDependencies;

Expand Down Expand Up @@ -313,6 +317,96 @@ public void generateVersionsJson_versionsGeneratedFromPackageJson_containsBothDe
versionsJson.toJson());
}

@Test
public void testGetPlatformPinnedDependencies_vaadinCoreVersionIsNotPresent_outputIsEmptyJson()
throws IOException {
Logger logger = Mockito.spy(Logger.class);
try (MockedStatic<LoggerFactory> loggerFactoryMocked = Mockito
.mockStatic(LoggerFactory.class)) {
loggerFactoryMocked
.when(() -> LoggerFactory.getLogger(nodeUpdater.getClass()))
.thenReturn(logger);

Mockito.when(
finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(null);
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
.thenReturn(null);

JsonObject pinnedVersions = nodeUpdater
.getPlatformPinnedDependencies();
Assert.assertEquals(0, pinnedVersions.keys().length);

Mockito.verify(logger, Mockito.times(1)).info(
"Couldn't find {} file to pin dependency versions for core components."
+ " Transitive dependencies won't be pinned for npm/pnpm.",
Constants.VAADIN_CORE_VERSIONS_JSON);
}
}

@Test
public void testGetPlatformPinnedDependencies_onlyVaadinCoreVersionIsPresent_outputContainsOnlyCoreVersions()
throws IOException {
File coreVersionsFile = File.createTempFile("vaadin-core-versions",
".json", temporaryFolder.newFolder());
JsonObject mockedVaadinCoreJson = getMockVaadinCoreVersionsJson();
Assert.assertTrue(mockedVaadinCoreJson.hasKey("core"));
Assert.assertTrue(
mockedVaadinCoreJson.getObject("core").hasKey("button"));
Assert.assertFalse(mockedVaadinCoreJson.hasKey("vaadin"));

FileUtils.write(coreVersionsFile, mockedVaadinCoreJson.toJson(),
StandardCharsets.UTF_8);
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(coreVersionsFile.toURI().toURL());
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
.thenReturn(null);

JsonObject pinnedVersions = nodeUpdater.getPlatformPinnedDependencies();

Assert.assertTrue(pinnedVersions.hasKey("@vaadin/button"));
Assert.assertFalse(pinnedVersions.hasKey("@vaadin/grid-pro"));
Assert.assertFalse(pinnedVersions.hasKey("@vaadin/vaadin-grid-pro"));
}

@Test
public void testGetPlatformPinnedDependencies_VaadinAndVaadinCoreVersionsArePresent_outputContainsBothCoreAndCommercialVersions()
throws IOException {
File coreVersionsFile = File.createTempFile("vaadin-core-versions",
".json", temporaryFolder.newFolder());
JsonObject mockedVaadinCoreJson = getMockVaadinCoreVersionsJson();
Assert.assertTrue(mockedVaadinCoreJson.hasKey("core"));
Assert.assertTrue(
mockedVaadinCoreJson.getObject("core").hasKey("button"));
Assert.assertFalse(mockedVaadinCoreJson.hasKey("vaadin"));

FileUtils.write(coreVersionsFile, mockedVaadinCoreJson.toJson(),
StandardCharsets.UTF_8);
Mockito.when(finder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(coreVersionsFile.toURI().toURL());

File vaadinVersionsFile = File.createTempFile("vaadin-versions",
".json", temporaryFolder.newFolder());
JsonObject mockedVaadinJson = getMockVaadinVersionsJson();
Assert.assertFalse(mockedVaadinJson.hasKey("core"));
Assert.assertTrue(mockedVaadinJson.hasKey("vaadin"));
Assert.assertTrue(
mockedVaadinJson.getObject("vaadin").hasKey("grid-pro"));
Assert.assertTrue(
mockedVaadinJson.getObject("vaadin").hasKey("vaadin-grid-pro"));

FileUtils.write(vaadinVersionsFile, mockedVaadinJson.toJson(),
StandardCharsets.UTF_8);
Mockito.when(finder.getResource(Constants.VAADIN_VERSIONS_JSON))
.thenReturn(vaadinVersionsFile.toURI().toURL());

JsonObject pinnedVersions = nodeUpdater.getPlatformPinnedDependencies();

Assert.assertTrue(pinnedVersions.hasKey("@vaadin/button"));
Assert.assertTrue(pinnedVersions.hasKey("@vaadin/grid-pro"));
Assert.assertTrue(pinnedVersions.hasKey("@vaadin/vaadin-grid-pro"));
}

private String getPolymerVersion(JsonObject object) {
JsonObject deps = object.get("dependencies");
String version = deps.getString("@polymer/polymer");
Expand All @@ -334,4 +428,91 @@ private void resolveResource_unhappyPath(String resourceFolder) {
Assert.assertEquals("foo", nodeUpdater.resolveResource("foo"));
Assert.assertEquals("foo", nodeUpdater.resolveResource("foo"));
}

private JsonObject getMockVaadinCoreVersionsJson() {
// @formatter:off
return Json.parse(
"{\n" +
" \"bundles\": {\n" +
" \"vaadin\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/bundles\"\n" +
" }\n" +
" },\n" +
" \"core\": {\n" +
" \"accordion\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/accordion\"\n" +
" },\n" +
" \"app-layout\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/app-layout\"\n" +
" },\n" +
" \"avatar\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/avatar\"\n" +
" },\n" +
" \"avatar-group\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/avatar-group\"\n" +
" },\n" +
" \"button\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/button\"\n" +
" },\n" +
" \"checkbox\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/checkbox\"\n" +
" }" +
" },\n" +
" \"platform\": \"23.2.0\"\n" +
"}"
);
// @formatter:on
}

private JsonObject getMockVaadinVersionsJson() {
// @formatter:off
return Json.parse(
"{\n" +
" \"vaadin\": {\n" +
" \"board\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/board\"\n" +
" },\n" +
" \"charts\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/charts\"\n" +
" },\n" +
" \"grid-pro\": {\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/grid-pro\"\n" +
" },\n" +
" \"vaadin-board\": {\n" +
" \"component\": true,\n" +
" \"javaVersion\": \"23.2.0\",\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/vaadin-board\",\n" +
" \"pro\": true\n" +
" },\n" +
" \"vaadin-charts\": {\n" +
" \"component\": true,\n" +
" \"javaVersion\": \"23.2.0\",\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/vaadin-charts\",\n" +
" \"pro\": true\n" +
" },\n" +
" \"vaadin-grid-pro\": {\n" +
" \"component\": true,\n" +
" \"javaVersion\": \"23.2.0\",\n" +
" \"jsVersion\": \"23.2.0\",\n" +
" \"npmName\": \"@vaadin/vaadin-grid-pro\",\n" +
" \"pro\": true\n" +
" },\n" +
" },\n" +
" \"platform\": \"23.2.0\"\n" +
"}"
);
// @formatter:on
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ public void runPnpmInstall_versionsJsonIsFound_pnpmHookFileIsGenerated()
ClassFinder classFinder = getClassFinder();
File versions = temporaryFolder.newFile();
FileUtils.write(versions, "{}", StandardCharsets.UTF_8);
Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(
classFinder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versions.toURI().toURL());

TaskRunNpmInstall task = createTask();
Expand Down Expand Up @@ -491,11 +492,12 @@ public void runPnpmInstall_userVersionNewerThanPinned_installedOverlayVersionIsN
final VersionsJsonFilter versionsJsonFilter = new VersionsJsonFilter(
Json.parse(packageJsonContent), NodeUpdater.DEPENDENCIES);
// Platform defines a pinned version
TaskRunNpmInstall task = createTask(
versionsJsonFilter.getFilteredVersions(
TaskRunNpmInstall task = createTask(versionsJsonFilter
.getFilteredVersions(
Json.parse("{ \"@vaadin/vaadin-overlay\":\""
+ PINNED_VERSION + "\"}"))
.toJson());
+ PINNED_VERSION + "\"}"),
"test-versions.json")
.toJson());
task.execute();

File overlayPackageJson = new File(getNodeUpdater().nodeModulesFolder,
Expand Down Expand Up @@ -538,11 +540,12 @@ public void runPnpmInstall_frameworkCollectedVersionNewerThanPinned_installedOve
final VersionsJsonFilter versionsJsonFilter = new VersionsJsonFilter(
Json.parse(packageJsonContent), NodeUpdater.DEPENDENCIES);
// Platform defines a pinned version
TaskRunNpmInstall task = createTask(
versionsJsonFilter.getFilteredVersions(
TaskRunNpmInstall task = createTask(versionsJsonFilter
.getFilteredVersions(
Json.parse("{ \"@vaadin/vaadin-overlay\":\""
+ PINNED_VERSION + "\"}"))
.toJson());
+ PINNED_VERSION + "\"}"),
"test-versions.json")
.toJson());
task.execute();

File overlayPackageJson = new File(getNodeUpdater().nodeModulesFolder,
Expand Down Expand Up @@ -644,7 +647,8 @@ protected TaskRunNpmInstall createTask(String versionsContent) {
private JsonObject getGeneratedVersionsContent(File versions)
throws IOException {
ClassFinder classFinder = getClassFinder();
Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON))
Mockito.when(
classFinder.getResource(Constants.VAADIN_CORE_VERSIONS_JSON))
.thenReturn(versions.toURI().toURL());

String path = getNodeUpdater().generateVersionsJson();
Expand Down
Loading

0 comments on commit 7462cbe

Please sign in to comment.