Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/us/kolmafia/multitool/Multitool.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,10 @@ private static ToolData processTool(String toolName) {
int localVersion = 0;
for (String systemJarName : locals) {
String jarName = systemJarName.toLowerCase();
int i = jarName.indexOf(toolName);
String hold = jarName.substring(i + toolName.length() + 1);
i = hold.indexOf(".jar");
hold = hold.substring(0, i);
if (hold.contains("-m")) {
i = hold.indexOf("-m");
hold = hold.substring(0, i);
retVal.setLocalModificationFound(true);
}
VersionData verDat = getVersionDataFromFilename(jarName, toolName);
runMe = systemJarName;
localVersion = Integer.parseInt(hold);
localVersion = verDat.getVersion();
retVal.setLocalModificationFound(verDat.isModified());
}
retVal.setCurrentVersion(localVersion);
retVal.setNeedToDownload(localVersion < version);
Expand Down Expand Up @@ -216,7 +209,7 @@ private static int getLatestReleaseVersion(String tool) {

/**
* Uses an opened input stream to determine the latest version of a tool in a remote repository.
* Caller needs to Closes the input stream.
* Caller needs to close the input stream.
*
* @param is Successfully opened input stream to remote repository.
* @return latest version in repository or zero
Expand Down Expand Up @@ -304,4 +297,33 @@ public static void initLogOrExit() {
System.exit(0);
}
}

static VersionData getVersionDataFromFilename(String jarName, String toolName) {
VersionData noResult = new VersionData(0, false);
jarName = jarName.toLowerCase();
toolName = toolName.toLowerCase();
String dotJar = ".jar";
if (!jarName.startsWith(toolName)) return noResult;
if (!jarName.endsWith(dotJar)) return noResult;
boolean mod = false;
int i = jarName.indexOf(toolName);
String hold = jarName.substring(i + toolName.length());
if (!hold.startsWith("-")) return noResult;
hold = hold.substring(1);
i = hold.indexOf(".jar");
hold = hold.substring(0, i);
if (hold.contains("-m")) {
i = hold.indexOf("-m");
hold = hold.substring(0, i);
mod = true;
}
boolean isNumeric = hold.chars().allMatch(Character::isDigit);
if (!isNumeric) return noResult;
try {
int verVal = Integer.parseInt(hold);
return new VersionData(verVal, mod);
} catch (NumberFormatException e) {
return noResult;
}
}
}
19 changes: 19 additions & 0 deletions src/us/kolmafia/multitool/VersionData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package us.kolmafia.multitool;

public class VersionData {
private final int version;
private final boolean modified;

public VersionData(int ver, boolean mod) {
this.version = ver;
this.modified = mod;
}

public int getVersion() {
return version;
}

public boolean isModified() {
return modified;
}
}
32 changes: 23 additions & 9 deletions test/us/kolmafia/multitool/MultitoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static us.kolmafia.multitool.Constants.ROOT_LOCATION;
import static us.kolmafia.multitool.Multitool.cleanPath;
import static us.kolmafia.multitool.Multitool.cwd;
import static us.kolmafia.multitool.Multitool.getVersionDataFromFilename;
import static us.kolmafia.multitool.Multitool.getVersionFromInputStream;
import static us.kolmafia.multitool.Multitool.initLogOrExit;
import static us.kolmafia.multitool.Multitool.logFileName;
Expand All @@ -28,6 +29,8 @@
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class MultitoolTest {

Expand All @@ -50,14 +53,7 @@ public void itShouldFindNoFiles() {

private boolean validateDestination(Path destination) {
File dFile = destination.toFile();
boolean retVal = dFile.exists();
if (retVal) {
retVal = dFile.isDirectory();
if (retVal) {
retVal = dFile.canWrite();
}
}
return retVal;
return dFile.exists() && dFile.isDirectory() && dFile.canWrite();
}

@Test
Expand All @@ -80,7 +76,7 @@ public void itShouldFindFilesThatWerePutThere() {
try {
copy(sPath, dPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
fail(e.getMessage());
}
}
inDir = processDirectory(KOLMAFIA_NAME);
Expand Down Expand Up @@ -131,4 +127,22 @@ public void itShouldGetKoLmafiaVersion() {
}
assertEquals(28465, getVersionFromInputStream(fs));
}

@ParameterizedTest
@CsvSource({
"kolmafia, kolmafia, 0, false",
"kolmafia-123, kolmafia, 0, false",
"kolmafia123.jar, kolmafia, 0, false",
"kolmafia-123.jar, kolmafia, 123, false",
"kolmafia-123-m.jar, kolmafia, 123, true",
"kolmafia-123-.jar, kolmafia, 0, false",
"kolmafia-latest.jar, kolmafia, 0, false",
"notatool-123.jar, kolmafia, 0, false"
})
public void itShouldGetVersions(
String jarName, String toolName, int expectedVersion, boolean expectedmod) {
VersionData vd = getVersionDataFromFilename(jarName, toolName);
assertEquals(expectedVersion, vd.getVersion());
assertEquals(expectedmod, vd.isModified());
}
}
14 changes: 14 additions & 0 deletions test/us/kolmafia/multitool/VersionDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package us.kolmafia.multitool;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class VersionDataTest {
@Test
public void itShouldGetWhatWasSet() {
VersionData vd = new VersionData(1066, true);
assertEquals(1066, vd.getVersion());
assertTrue(vd.isModified());
}
}