Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 6546c5b

Browse files
author
mikkomaa
authored
Merge branch 'master' into validatoin-results-in-submit-juha
2 parents b82ff1d + d3f2bd5 commit 6546c5b

File tree

5 files changed

+49
-43
lines changed

5 files changed

+49
-43
lines changed

src/main/java/fi/helsinki/cs/tmc/cli/Application.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public void printHelp(String description) {
113113
public void run(String[] args) {
114114
context.setApp(this);
115115

116-
if (!context.inTests()) {
117-
versionCheck();
116+
if (!context.inTests() && versionCheck()) {
117+
return;
118118
}
119119

120120
String[] commandArgs = parseArgs(args);
@@ -134,7 +134,7 @@ public static void main(String[] args) {
134134
app.run(args);
135135
}
136136

137-
private void versionCheck() {
137+
private boolean versionCheck() {
138138
Map<String, String> properties = context.getProperties();
139139
String previousTimestamp = properties.get(previousUpdateDateKey);
140140
Date previous = null;
@@ -146,23 +146,25 @@ private void versionCheck() {
146146
} catch (NumberFormatException ex) {
147147
io.println("The previous update date isn't number.");
148148
logger.warn("The previous update date isn't number.", ex);
149-
return;
149+
return false;
150150
}
151151
previous = new Date(time);
152152
}
153153

154154
Date now = new Date();
155155
if (previous != null && previous.getTime() + defaultUpdateInterval > now.getTime()) {
156-
return;
156+
return false;
157157
}
158158

159159
TmcCliUpdater update = new TmcCliUpdater(io, EnvironmentUtil.getVersion(),
160160
EnvironmentUtil.isWindows());
161-
update.run();
161+
boolean updated = update.run();
162162

163163
long timestamp = now.getTime();
164164
properties.put(previousUpdateDateKey, Long.toString(timestamp));
165165
context.saveProperties();
166+
167+
return updated;
166168
}
167169

168170
//TODO rename this as getColorProperty

src/main/java/fi/helsinki/cs/tmc/cli/command/core/CommandFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public class CommandFactory {
2020
private static final Map<String, Class<Command>> commands = new HashMap<>();
2121

2222
static {
23-
/* force load the CommandList so that it's static initialization block is executed
24-
this is used instead of import so that the ide's won't cry about the nonexistent class
23+
/* Force load the CommandList so that it's static initialization block is executed.
24+
* This hack is used instead of import so that the IDEs won't cry about the nonexistent
25+
* class.
2526
*/
2627
try {
2728
Class.forName("fi.helsinki.cs.tmc.cli.command.core.CommandList");

src/main/java/fi/helsinki/cs/tmc/cli/io/ExternalsUtil.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,19 @@ public static void showFileInPager(Path file) {
9797
}
9898
}
9999

100-
public static void runUpdater(Io io, String pathToNewBinary) {
101-
if (!execExternal("chmod u+x", pathToNewBinary, true)) {
100+
public static boolean runUpdater(Io io, String pathToNewBinary) {
101+
if (!ExternalsUtil.execExternal("chmod u+x " + pathToNewBinary, true)) {
102102
logger.error("Failed to set execution permissions to the new binary");
103-
return;
103+
io.println("Failed to set execution permissions to the new binary");
104+
return false;
104105
}
105-
if (!execExternal(pathToNewBinary, "++internal-update", true)) {
106+
if (!ExternalsUtil.execExternal(pathToNewBinary + " ++internal-update", true)) {
106107
io.println("Failed to run the tmc-cli at " + pathToNewBinary);
107108
io.println("Run it with ++internal-update argument or contact the help desk");
108109
logger.error("Failed to run the new tmc");
110+
return false;
109111
}
112+
return true;
110113
}
111114

112115
/**

src/main/java/fi/helsinki/cs/tmc/cli/updater/TmcCliUpdater.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010

1111
import org.apache.commons.io.FileUtils;
1212
import org.apache.commons.io.IOUtils;
13-
import org.apache.http.HttpEntity;
14-
import org.apache.http.client.methods.CloseableHttpResponse;
15-
import org.apache.http.client.methods.HttpGet;
16-
import org.apache.http.impl.client.CloseableHttpClient;
17-
import org.apache.http.impl.client.HttpClients;
1813
import org.slf4j.Logger;
1914
import org.slf4j.LoggerFactory;
2015

2116
import java.io.File;
17+
import java.io.InputStream;
2218
import java.io.IOException;
19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
import java.net.URLConnection;
2322

2423
public class TmcCliUpdater {
2524

@@ -46,73 +45,73 @@ public TmcCliUpdater(Io io, String currentVersion, boolean isWindows) {
4645
* Checks if there's a newer tmc-cli version released on Github and asks if
4746
* the user wants to download it. TODO: split it up
4847
*/
49-
public void run() {
48+
public boolean run() {
5049
JsonObject release = toJsonObject(fetchLatestReleaseJson());
5150
if (release == null || !isNewer(release)) {
52-
return;
51+
return false;
5352
}
5453

5554
JsonObject binAsset = findCorrectAsset(release, isWindows);
5655
if (binAsset == null || !binAsset.has("name") || !binAsset.has("browser_download_url")) {
5756
logger.warn("The JSON does not contain necessary information for update.");
58-
return;
57+
return false;
5958
}
6059

6160
io.println("A new version of tmc-cli is available!");
6261

6362
if (isWindows) { //just show a link for Windows users now, todo...
6463
io.println("Download: https://github.com/tmc-cli/tmc-cli/releases/latest");
65-
return;
64+
return false;
6665
}
6766

6867
if (! io.readConfirmation("Do you want to download it?", true)) {
69-
return;
68+
return false;
7069
}
7170

7271
String binName = binAsset.get("name").getAsString() + ".new";
7372
String dlUrl = binAsset.get("browser_download_url").getAsString();
7473
String currentBinLocation = getJarLocation();
7574
if (currentBinLocation == null) {
7675
io.println("Unable to find current program location, aborting update.");
77-
return;
76+
return false;
7877
}
7978
File destination = new File(currentBinLocation + binName);
8079

8180
io.println("Downloading...");
8281
fetchTmcCliBinary(dlUrl, destination);
8382

8483
io.println("Running " + destination.getAbsolutePath());
85-
runNewTmcCliBinary(destination.getAbsolutePath());
84+
return runNewTmcCliBinary(destination.getAbsolutePath());
8685
}
8786

88-
protected byte[] fetchHttpEntity(String url) {
89-
CloseableHttpClient httpClient = HttpClients.createDefault();
90-
HttpGet httpGet = new HttpGet(url);
91-
httpGet.addHeader("User-Agent", "tmc-cli (https://github.com/tmc-cli/tmc-cli)");
87+
protected byte[] fetchHttpEntity(String urlAddress) {
88+
URL url;
9289

93-
HttpEntity entity;
94-
byte[] content;
9590
try {
96-
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
97-
entity = httpResponse.getEntity();
98-
if (entity == null) {
99-
logger.warn("Failed to get http request content.");
100-
httpGet.releaseConnection();
101-
return null;
102-
}
91+
url = new URL(urlAddress);
92+
} catch (MalformedURLException ex) {
93+
logger.warn("Url formatting failed", ex);
94+
return null;
95+
}
10396

97+
InputStream inputStream;
98+
try {
99+
URLConnection connection = url.openConnection();
100+
connection.setRequestProperty("User-Agent", "tmc-cli (https://github.com/tmc-cli/tmc-cli)");
101+
inputStream = connection.getInputStream();
104102
} catch (IOException ex) {
105-
logger.warn("Failed to create http connection to github.", ex);
103+
logger.warn("Failed to fetch page", ex);
106104
io.println("Failed to create https connection to github.");
107105
return null;
108106
}
107+
108+
byte[] content;
109109
try {
110-
content = IOUtils.toByteArray(entity.getContent());
110+
content = IOUtils.toByteArray(inputStream);
111111
} catch (IOException ex) {
112112
logger.warn("Failed to fetch data from github", ex);
113113
content = null;
114114
}
115-
httpGet.releaseConnection();
116115
return content;
117116
}
118117

@@ -153,8 +152,8 @@ protected void fetchTmcCliBinary(String downloadUrl, File destination) {
153152
/**
154153
* Finish the update by running downloaded binary.
155154
*/
156-
protected void runNewTmcCliBinary(String pathToNewBinary) {
157-
ExternalsUtil.runUpdater(io, pathToNewBinary);
155+
protected boolean runNewTmcCliBinary(String pathToNewBinary) {
156+
return ExternalsUtil.runUpdater(io, pathToNewBinary);
158157
}
159158

160159
/**

src/test/java/fi/helsinki/cs/tmc/cli/updater/TmcCliUpdaterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static org.mockito.Mockito.spy;
1313
import static org.mockito.Mockito.times;
1414
import static org.mockito.Mockito.verify;
15+
import static org.mockito.Mockito.when;
1516

1617
import fi.helsinki.cs.tmc.cli.io.TestIo;
1718

@@ -107,7 +108,7 @@ public void downloadsAndRunsNewBinaryIfOk() {
107108
doReturn(latestJson).when(updater).fetchLatestReleaseJson();
108109
//when(updater.fetchLatestReleaseJson()).thenReturn(latestJson);
109110
doNothing().when(updater).fetchTmcCliBinary(any(String.class), any(File.class));
110-
doNothing().when(updater).runNewTmcCliBinary(any(String.class));
111+
when(updater.runNewTmcCliBinary(any(String.class))).thenReturn(true);
111112
updater.run();
112113
assertThat(io.out(), containsString("A new version of tmc-cli is available!"));
113114
assertThat(io.out(), containsString("Downloading..."));

0 commit comments

Comments
 (0)