Skip to content

Commit

Permalink
Always use a Reader.
Browse files Browse the repository at this point in the history
  • Loading branch information
waynebeaton committed Feb 24, 2025
1 parent 2fcd055 commit e834607
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
29 changes: 15 additions & 14 deletions core/src/main/java/org/eclipse/dash/licenses/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,26 @@ private OutputStream getWriter(String path) throws FileNotFoundException {
return new FileOutputStream(new File(path));
}

@SuppressWarnings("resource")
private IDependencyListReader getReader(String name) throws FileNotFoundException {
InputStreamReader input;
if ("-".equals(name)) {
return new FlatFileReader(new InputStreamReader(System.in));
input = new InputStreamReader(System.in);
} else {
File input = new File(name);
if (input.exists()) {
switch (input.getName()) {
case "pnpm-lock.yaml":
return new PnpmPackageLockFileReader(new FileInputStream(input));
case "package-lock.json":
return new PackageLockFileReader(new FileInputStream(input));
case "yarn.lock":
return new YarnLockFileReader(new FileReader(input));
}
return new FlatFileReader(new FileReader(input));
} else {
File file = new File(name);
if (!file.exists()) {
throw new FileNotFoundException(name);
}

input = new FileReader(file);
switch (file.getName()) {
case "pnpm-lock.yaml":
return new PnpmPackageLockFileReader(input);
case "package-lock.json":
return new PackageLockFileReader(input);
case "yarn.lock":
return new YarnLockFileReader(input);
}
}
return new FlatFileReader(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*************************************************************************/
package org.eclipse.dash.licenses.cli;

import java.io.InputStream;
import java.io.Reader;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
Expand All @@ -32,10 +32,10 @@ public class PackageLockFileReader implements IDependencyListReader {
final Logger logger = LoggerFactory.getLogger(PackageLockFileReader.class);
private static final Pattern Name_Pattern = Pattern.compile("(?:(?<scope>@[^\\/]+)\\/)?(?<name>[^\\/]+)$");

private final InputStream input;
private final Reader input;
private JsonObject json;

public PackageLockFileReader(InputStream input) {
public PackageLockFileReader(Reader input) {
this.input = input;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package org.eclipse.dash.licenses.cli;

import java.io.InputStream;
import java.io.Reader;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -46,14 +46,14 @@ 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;
private final Reader 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) {
public PnpmPackageLockFileReader(Reader input) {
this.input = input;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
Expand All @@ -32,7 +33,7 @@ class PackageLockFileReaderTests {
@Test
void testV1Format() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream(PACKAGE_LOCK_JSON)) {
PackageLockFileReader reader = new PackageLockFileReader(input);
PackageLockFileReader reader = new PackageLockFileReader(new InputStreamReader(input));
Collection<IContentId> ids = reader.getContentIds();

IContentId[] includes = { ContentId.getContentId("npm/npmjs/-/loglevel/1.6.1"),
Expand All @@ -50,7 +51,7 @@ void testV1Format() throws IOException {
@Test
void testV2Format() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream(PACKAGE_LOCK_V2_JSON)) {
PackageLockFileReader reader = new PackageLockFileReader(input);
PackageLockFileReader reader = new PackageLockFileReader(new InputStreamReader(input));
Collection<IContentId> ids = reader.getContentIds();

assertTrue(ids.stream().allMatch(each -> each.isValid()));
Expand All @@ -69,7 +70,7 @@ void testV2Format() throws IOException {
@Test
void testV2FormatWithWorkspaces() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/test_data_package-lock-v2-2.json")) {
PackageLockFileReader reader = new PackageLockFileReader(input);
PackageLockFileReader reader = new PackageLockFileReader(new InputStreamReader(input));
var ids = reader.getContentIds();

assertTrue(ids.stream().allMatch(each -> each.isValid()));
Expand All @@ -84,7 +85,7 @@ void testV2FormatWithWorkspaces() throws IOException {
@Test
void testV3Format() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/test_data_package-lock-v3.json")) {
PackageLockFileReader reader = new PackageLockFileReader(input);
PackageLockFileReader reader = new PackageLockFileReader(new InputStreamReader(input));
var ids = reader.contentIds().collect(Collectors.toList());

assertTrue(ids.stream().allMatch(each -> each.isValid()));
Expand Down Expand Up @@ -118,7 +119,7 @@ void testV3Format() throws IOException {
@Test
void testV3FormatWorkspaceIgnore() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/test_data_package-lock-v3-theia.json")) {
PackageLockFileReader reader = new PackageLockFileReader(input);
PackageLockFileReader reader = new PackageLockFileReader(new InputStreamReader(input));
var ids = reader.contentIds().collect(Collectors.toList());

assertTrue(ids.stream().allMatch(each -> each.isValid()));
Expand Down Expand Up @@ -148,7 +149,7 @@ void testV3FormatWorkspaceIgnore() throws IOException {
@Test
void testAllRecordsDetected() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/differentResolved.json")) {
PackageLockFileReader reader = new PackageLockFileReader(input);
PackageLockFileReader reader = new PackageLockFileReader(new InputStreamReader(input));

String[] expected = { "npm/npmjs/@babel/code-frame/7.12.13", "npm/local/-/some_local_package/1.2.3", };
Arrays.sort(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;

Expand All @@ -30,7 +31,7 @@ class PnpmPackageLockFileReaderTests {
@Test
void testNoPackages() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/fixtures/pnpm/pnpm-lock-no-packages.yaml")) {
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(input);
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(new InputStreamReader(input));
var ids = reader.getContentIds();
assertTrue(ids.isEmpty());
}
Expand All @@ -39,7 +40,7 @@ void testNoPackages() throws IOException {
@Test
void testDuplicates() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/fixtures/pnpm/pnpm-lock-duplicate.yaml")) {
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(input);
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(new InputStreamReader(input));
var ids = reader.getContentIds();
assertEquals(1, ids.size());
assertEquals("npm/npmjs/@babel/preset-modules/0.1.6-no-external-plugins", ids.iterator().next().toString());
Expand All @@ -49,7 +50,7 @@ void testDuplicates() throws IOException {
@Test
void testV5Format() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/fixtures/pnpm/pnpm-lock-v5.yaml")) {
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(input);
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(new InputStreamReader(input));
var ids = reader.getContentIds();

assertEquals(12, ids.size());
Expand All @@ -68,7 +69,7 @@ void testV5Format() throws IOException {
@Test
void testV6Format() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/fixtures/pnpm/pnpm-lock-v6.yaml")) {
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(input);
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(new InputStreamReader(input));
var ids = reader.getContentIds();

assertEquals(579, ids.size());
Expand All @@ -87,7 +88,7 @@ void testV6Format() throws IOException {
@Test
void testV9Format() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/fixtures/pnpm/pnpm-lock-v9.yaml")) {
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(input);
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(new InputStreamReader(input));
var ids = reader.getContentIds();

assertEquals(12, ids.size());
Expand All @@ -106,7 +107,7 @@ void testV9Format() throws IOException {
@Test
void testAllRecordsDetected() throws IOException {
try (InputStream input = this.getClass().getResourceAsStream("/fixtures/pnpm/pnpm-lock-v6-small.yaml")) {
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(input);
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(new InputStreamReader(input));

String[] expected = { "npm/npmjs/-/git-semver-tags/4.1.1", "npm/npmjs/@babel/code-frame/7.18.6",
"npm/npmjs/@babel/preset-modules/0.1.6-no-external-plugins" };
Expand All @@ -119,7 +120,7 @@ void testAllRecordsDetected() throws IOException {
@Test
void shouldReturnErrorForInvalidYamlfile() {
InputStream input = new ByteArrayInputStream("invalid".getBytes(Charset.defaultCharset()));
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(input);
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(new InputStreamReader(input));

Exception exception = assertThrows(RuntimeException.class, reader::getContentIds);
assertEquals("Error reading content of package-lock.yaml file", exception.getMessage());
Expand All @@ -128,7 +129,7 @@ void shouldReturnErrorForInvalidYamlfile() {
@Test
void shouldReturnErrorForEmptyfile() {
InputStream input = new ByteArrayInputStream("".getBytes(Charset.defaultCharset()));
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(input);
PnpmPackageLockFileReader reader = new PnpmPackageLockFileReader(new InputStreamReader(input));

Exception exception = assertThrows(RuntimeException.class, reader::getContentIds);
assertEquals("Error reading content of package-lock.yaml file", exception.getMessage());
Expand Down

0 comments on commit e834607

Please sign in to comment.