Skip to content

Commit

Permalink
Rework tests so that they actually run as part of the build
Browse files Browse the repository at this point in the history
  • Loading branch information
waynebeaton committed Apr 23, 2024
1 parent c9021c7 commit 8b1cbf9
Show file tree
Hide file tree
Showing 31 changed files with 160 additions and 152 deletions.
1 change: 1 addition & 0 deletions core/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
46 changes: 29 additions & 17 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
* SPDX-License-Identifier: EPL-2.0
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.dash</groupId>
Expand Down Expand Up @@ -42,13 +44,14 @@
</organization>

<name>Eclipse Dash License Tool Core and CLI</name>
<description>The Eclipse Dash License Tool identifies the licenses of content.</description>
<description>The Eclipse Dash License Tool identifies the licenses of
content.</description>
<url>https://github.com/eclipse/dash-licenses</url>

<scm>
<url>https://github.com/eclipse/dash-licenses</url>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>

<issueManagement>
<system>GitHub</system>
Expand Down Expand Up @@ -120,19 +123,19 @@
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.10.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.10.2</version>
<scope>test</scope>
</dependency>
Expand All @@ -145,15 +148,24 @@
<version>3.2.5</version>
<configuration>
<test>org.eclipse.dash.licenses.tests.TestSuite</test>
<additionalClasspathElements>
<additionalClasspathElement>${basedir}/src/test/java</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.5</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.10.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.10.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

package org.eclipse.dash.licenses.cli;

import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.dash.licenses.ContentId;
import org.eclipse.dash.licenses.IContentId;
import org.eclipse.dash.licenses.InvalidContentId;
Expand All @@ -21,104 +30,97 @@
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.representer.Representer;

import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* This class is responsible for reading a pnpm-lock.yaml generated by
* the PNPM Package Manager and extracting content IDs.
* A content ID represents a unique identifier for a package or dependency.
* This class is responsible for reading a pnpm-lock.yaml generated by the PNPM
* Package Manager and extracting content IDs. A content ID represents a unique
* identifier for a package or dependency.
*
* The class implements the IDependencyListReader interface.
*
* The class uses the SnakeYAML library to parse the pnpm-lock.yaml file.
* Content ID is extracted only from the keys of the packages section of the pnpm-lock file.
* The main magic is done by the regex: KEY_PATTERN.
* Content ID is extracted only from the keys of the packages section of the
* pnpm-lock file. The main magic is done by the regex: KEY_PATTERN.
*
**/
public class PnpmPackageLockFileReader implements IDependencyListReader {
final Logger logger = LoggerFactory.getLogger(PnpmPackageLockFileReader.class);
private static final Pattern KEY_PATTERN = Pattern.compile("^'?(\\/?(?<namespace>@[^\\/]+)\\/)?\\/?(?<name>[^\\/@]+)[@\\/](?<version>[^(@\\/'\\n]+)(?=\\()?");
private final InputStream input;
final Logger logger = LoggerFactory.getLogger(PnpmPackageLockFileReader.class);
private static final Pattern KEY_PATTERN = Pattern
.compile("^'?(\\/?(?<namespace>@[^\\/]+)\\/)?\\/?(?<name>[^\\/@]+)[@\\/](?<version>[^(@\\/'\\n]+)(?=\\()?");
private final InputStream input;

/**
* Constructs a new PnpmPackageLockFileReader with the specified input stream.
*
* @param input the input stream of the PNPM package-lock file
*/
public PnpmPackageLockFileReader(InputStream input) {
this.input = input;
}
/**
* Constructs a new PnpmPackageLockFileReader with the specified input stream.
*
* @param input the input stream of the PNPM package-lock file
*/
public PnpmPackageLockFileReader(InputStream input) {
this.input = input;
}

/**
* Returns a collection of unique content IDs extracted from the PNPM package-lock file.
*
* @return a collection of content IDs
*/
@Override
public Collection<IContentId> getContentIds() {
return contentIds().distinct().collect(Collectors.toList());
}
/**
* Returns a collection of unique content IDs extracted from the PNPM
* package-lock file.
*
* @return a collection of content IDs
*/
@Override
public Collection<IContentId> getContentIds() {
return contentIds().distinct().collect(Collectors.toList());
}

/**
* Parses the specified key and returns the corresponding content ID.
*
* @param key the key to parse
* @return the content ID extracted from the key
*/
public IContentId getId(String key) {
var matcher = KEY_PATTERN.matcher(key);
if (matcher.find()) {
var namespace = Optional.ofNullable(matcher.group("namespace")).orElse("-");
var name = matcher.group("name");
var version = matcher.group("version");
return ContentId.getContentId("npm", "npmjs", namespace, name, version);
}
/**
* Parses the specified key and returns the corresponding content ID.
*
* @param key the key to parse
* @return the content ID extracted from the key
*/
public IContentId getId(String key) {
var matcher = KEY_PATTERN.matcher(key);
if (matcher.find()) {
var namespace = Optional.ofNullable(matcher.group("namespace")).orElse("-");
var name = matcher.group("name");
var version = matcher.group("version");
return ContentId.getContentId("npm", "npmjs", namespace, name, version);
}

logger.debug("Invalid content id: {}", key);
return new InvalidContentId(key);
}
logger.debug("Invalid content id: {}", key);
return new InvalidContentId(key);
}

/**
* Returns a stream of content IDs extracted from the PNPM package-lock file.
* We only read the keys of the packages.
/**
* Returns a stream of content IDs extracted from the PNPM package-lock file. We
* only read the keys of the packages.
*
* packages:
*
* /@babel/preset-modules@0.1.6-no-external-plugins(@babel/core@7.23.2):
*
* @return a stream of content IDs
*/
* /@babel/preset-modules@0.1.6-no-external-plugins(@babel/core@7.23.2):
*
* @return a stream of content IDs
*/
@SuppressWarnings("unchecked")
public Stream<IContentId> contentIds() {
Yaml yaml = getYamlParser();
Map<String, Object> load;
try {
load = yaml.load(input);
Map<String, Object> packages = (Map<String, Object>) load.getOrDefault("packages", new LinkedHashMap<>());
return packages.keySet().stream().map(this::getId);
} catch (Exception e) {
logger.error("Error reading content of package-lock.yaml file", e);
throw new RuntimeException("Error reading content of package-lock.yaml file");
}
}
public Stream<IContentId> contentIds() {
Yaml yaml = getYamlParser();
Map<String, Object> load;
try {
load = yaml.load(input);
Map<String, Object> packages = (Map<String, Object>) load.getOrDefault("packages", new LinkedHashMap<>());
return packages.keySet().stream().map(this::getId);
} catch (Exception e) {
logger.debug("Error reading content of package-lock.yaml file", e);
throw new RuntimeException("Error reading content of package-lock.yaml file");
}
}

/**
* Returns a YAML parser with custom options.
*
* @return a YAML parser
*/
private static Yaml getYamlParser() {
Representer representer = new Representer(new DumperOptions());
representer.getPropertyUtils().setSkipMissingProperties(true);
LoaderOptions loaderOptions = new LoaderOptions();
SafeConstructor constructor = new SafeConstructor(loaderOptions);
return new Yaml(constructor, representer);
}
/**
* Returns a YAML parser with custom options.
*
* @return a YAML parser
*/
private static Yaml getYamlParser() {
Representer representer = new Representer(new DumperOptions());
representer.getPropertyUtils().setSkipMissingProperties(true);
LoaderOptions loaderOptions = new LoaderOptions();
SafeConstructor constructor = new SafeConstructor(loaderOptions);
return new Yaml(constructor, representer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*************************************************************************/
package org.eclipse.dash.licenses.tests;

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.eclipse.dash.licenses.ClearlyDefinedIdParser;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -40,8 +40,9 @@ void testInvalid3() {

@Test
void testInvalid4() {
assertNull(new ClearlyDefinedIdParser().parseId(
"p2/orbit/p2.eclipse-plugin/org.junit.jupiter.params/5.7.1.v20210222-1948, unknown, restricted, none"));
assertNull(new ClearlyDefinedIdParser()
.parseId(
"p2/orbit/p2.eclipse-plugin/org.junit.jupiter.params/5.7.1.v20210222-1948, unknown, restricted, none"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*************************************************************************/
package org.eclipse.dash.licenses.tests;

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.eclipse.dash.licenses.GolangIdParser;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit 8b1cbf9

Please sign in to comment.