Skip to content

Commit

Permalink
fix(yarn): Added support for name version entries that start with an …
Browse files Browse the repository at this point in the history
…@ symbol.
  • Loading branch information
taikuukaits committed Mar 12, 2020
1 parent 53baadd commit f9da1aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public YarnLock parseYarnLock(final List<String> yarnLockFileAsList) {
} else {
started = true;
}
ids = getFuzzyIdsFromLine(line);
ids = parseMultipleEntryLine(line);
} else if (level == 1 && trimmedLine.startsWith(VERSION_PREFIX)) {
resolvedVersion = getVersionFromLine(trimmedLine);
} else if (level == 1 && trimmedLine.startsWith(OPTIONAL_DEPENDENCIES_TOKEN)) {
Expand Down Expand Up @@ -91,18 +91,30 @@ private String removeWrappingQuotes(final String s) {
return StringUtils.removeStart(StringUtils.removeEnd(s.trim(), "\""), "\"");
}

public List<YarnLockEntryId> getFuzzyIdsFromLine(final String s) {
//Takes a line of the form "entry \"entry\" entry:"
public List<YarnLockEntryId> parseMultipleEntryLine(final String line) {
final List<YarnLockEntryId> ids = new ArrayList<>();
final String[] lines = s.split(",");
for (final String line : lines) {
final String cleanedLine = removeWrappingQuotes(StringUtils.removeEnd(line.trim(), ":"));
final String name = StringUtils.substringBefore(cleanedLine, "@");
final String version = StringUtils.substringAfter(cleanedLine, "@");
ids.add(new YarnLockEntryId(name, version));
final String[] entries = line.split(",");
for (final String entryRaw : entries) {
final String entryNoColon = StringUtils.removeEnd(entryRaw.trim(), ":");
final String entryNoColonOrQuotes = removeWrappingQuotes(entryNoColon);
YarnLockEntryId entry = parseSingleEntry(entryNoColonOrQuotes);
ids.add(entry);
}
return ids;
}

//Takes an entry of format "name@version" or "@name@version" where name has an @ symbol.
public YarnLockEntryId parseSingleEntry(String entry) {
if (StringUtils.countMatches(entry, "@") == 1 && entry.startsWith("@")) {
return new YarnLockEntryId(entry, "");
} else {
final String name = StringUtils.substringBeforeLast(entry, "@");
final String version = StringUtils.substringAfterLast(entry, "@");
return new YarnLockEntryId(name, version);
}
}

private String getVersionFromLine(final String line) {
final String rawVersion = line.substring(VERSION_PREFIX.length(), line.lastIndexOf(VERSION_SUFFIX));
return removeWrappingQuotes(rawVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,23 @@ void testThatDependenciesWithQuotesAreResolvedCorrectly() {
@Test
void testParserHandlesMissingSymbol() {
YarnLockParser yarnLockParser = new YarnLockParser();
List<YarnLockEntryId> ids = yarnLockParser.getFuzzyIdsFromLine("example, example@1");
List<YarnLockEntryId> ids = yarnLockParser.parseMultipleEntryLine("example, example@1");
Assertions.assertEquals(2, ids.size());
Assertions.assertEquals(ids.get(0).getName(), "example");
Assertions.assertEquals(ids.get(0).getVersion(), "");
Assertions.assertEquals(ids.get(1).getName(), "example");
Assertions.assertEquals(ids.get(1).getVersion(), "1");
}

@Test
void handlesSymbolInName() {
YarnLockParser yarnLockParser = new YarnLockParser();
List<YarnLockEntryId> ids = yarnLockParser.parseMultipleEntryLine("@example");
Assertions.assertEquals(1, ids.size());
Assertions.assertEquals(ids.get(0).getName(), "@example");
Assertions.assertEquals(ids.get(0).getVersion(), "");
}

void assertEntry(final YarnLock yarnLock, final String idName, final String idVersion, final String resolvedVersion, final YarnLockDependency... dependencies) {
boolean found = false;
for (final YarnLockEntry entry : yarnLock.getEntries()) {
Expand Down

0 comments on commit f9da1aa

Please sign in to comment.