-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCliToolsTest.java
More file actions
134 lines (111 loc) · 5.7 KB
/
Copy pathCliToolsTest.java
File metadata and controls
134 lines (111 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package appland.cli;
import appland.AppMapBaseTest;
import appland.AppMapDeploymentTestUtils;
import appland.deployment.AppMapDeploymentSettingsService;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.testFramework.fixtures.TempDirTestFixture;
import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.List;
public class CliToolsTest extends AppMapBaseTest {
@Override
public TempDirTestFixture createTempDirTestFixture() {
// create temp files on disk
return new TempDirTestFixtureImpl();
}
@Override
protected boolean runInDispatchThread() {
return false;
}
@Test
public void bestBinarySearch() throws Exception {
// Mock binaries to copy into the location where bundled binaries are searched
// The high version numbers ensure that a binary downloaded by our tests are not returned.
var appmapBinaries = List.of(
createMockBinary("appmap-win-x64-v1000.2.0.exe"),
createMockBinary("appmap-win-x64-v1000.1.123.exe"),
createMockBinary("appmap-win-arm64-v1000.2.0.exe"),
createMockBinary("appmap-win-arm64-v1000.1.123.exe"),
createMockBinary("appmap-linux-x64-v1000.2.0"),
createMockBinary("appmap-linux-x64-v1000.1.123"),
createMockBinary("appmap-linux-arm64-v1000.2.0"),
createMockBinary("appmap-linux-arm64-v1000.1.123"),
createMockBinary("appmap-macos-x64-v1000.2.0"),
createMockBinary("appmap-macos-x64-v1000.1.123"),
createMockBinary("appmap-macos-arm64-v1000.2.0"),
createMockBinary("appmap-macos-arm64-v1000.1.123")
);
AppMapDeploymentTestUtils.withBundledBinaries(appmapBinaries, () -> {
assertNotNull("There must be a match for every platform", CliTools.getBinaryPath(CliTool.AppMap));
var windowsMatch = CliTools.getBinaryPath(CliTool.AppMap, "win", "x64");
assertNotNull(windowsMatch);
assertEquals("appmap-win-x64-v1000.2.0.exe", windowsMatch.getFileName().toString());
var macosMatch = CliTools.getBinaryPath(CliTool.AppMap, "macos", "arm64");
assertNotNull(macosMatch);
assertEquals("appmap-macos-arm64-v1000.2.0", macosMatch.getFileName().toString());
if (SystemInfo.isMac) {
assertTrue(Files.isExecutable(macosMatch));
}
var linuxMatch = CliTools.getBinaryPath(CliTool.AppMap, "linux", "x64");
assertNotNull(linuxMatch);
assertEquals("appmap-linux-x64-v1000.2.0", linuxMatch.getFileName().toString());
if (SystemInfo.isLinux) {
assertTrue(Files.isExecutable(linuxMatch));
}
});
}
@Test
public void bundledBinaryIsPreferred() throws Exception {
TestAppLandDownloadService.ensureDownloaded();
var downloadedAppMapBinary = AppLandDownloadService.getInstance().getDownloadFilePath(CliTool.AppMap,
CliTools.currentPlatform(),
CliTools.currentArch());
assertNotNull(downloadedAppMapBinary);
var bundledBinary = createMockBinary(downloadedAppMapBinary.getFileName().toString());
AppMapDeploymentTestUtils.withBundledBinaries(List.of(bundledBinary), () -> {
var bestMatch = CliTools.getBinaryPath(CliTool.AppMap);
assertNotNull(bestMatch);
assertEquals(bundledBinary.getFileName().toString(), bestMatch.getFileName().toString());
var binaryParentDir = AppMapDeploymentSettingsService.bundledBinarySearchPath().stream().findFirst().orElse(null);
assertNotNull(binaryParentDir);
assertEquals(
"Bundled binaries should be preferred over downloaded binaries",
binaryParentDir.resolve(bestMatch.getFileName()),
bestMatch);
});
}
@Test
public void testBundledBinaryPermissionsFixed() throws Exception {
if (!SystemInfo.isUnix) {
return;
}
// Mock binary name that matches the current platform/arch
var binaryName = String.format("appmap-%s-%s-v1000.0.0", CliTools.currentPlatform(), CliTools.currentArch());
var mockBinary = createMockBinary(binaryName);
AppMapDeploymentTestUtils.withBundledBinaries(List.of(mockBinary), () -> {
// Find the file that was just copied
var bundledPath = AppMapDeploymentSettingsService.bundledBinarySearchPath().get(0).resolve(binaryName);
// Force it to be non-executable first
if (Files.isExecutable(bundledPath)) {
var perms = Files.getPosixFilePermissions(bundledPath);
perms.remove(PosixFilePermission.OWNER_EXECUTE);
perms.remove(PosixFilePermission.GROUP_EXECUTE);
perms.remove(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(bundledPath, perms);
}
assertFalse(Files.isExecutable(bundledPath));
// Now call the method under test
var binaryPath = CliTools.getBinaryPath(CliTool.AppMap);
assertNotNull(binaryPath);
assertEquals(binaryName, binaryPath.getFileName().toString());
assertTrue("Bundled binary must be made executable", Files.isExecutable(binaryPath));
});
}
private @NotNull Path createMockBinary(String filename) {
return myFixture.getTempDirFixture().createFile(filename).toNioPath();
}
}