-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
122 lines (100 loc) · 2.58 KB
/
Program.cs
File metadata and controls
122 lines (100 loc) · 2.58 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
//
// Run Docker/Linux test with:
//
// docker build -t sgr/findexecutable .
// docker run --rm sgr/findexecutable
//
using System;
using System.IO;
using System.Reflection;
namespace FindExecutable
{
internal class Program
{
static int Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
int retval = 0;
retval = Math.Max(retval, TestSearchGit());
retval = Math.Max(retval, TestUnicodeFile());
Console.WriteLine("done.");
if (retval != 0)
{
Console.WriteLine($"exit code: {retval}");
}
return retval;
}
static int TestSearchGit()
{
string[] executables = [
"git.exe",
"git"
];
foreach (string executable in executables)
{
try
{
Console.WriteLine($"Searching for: {executable}");
string? fullPath = FindExecutable.FullPath(executable);
if (fullPath != null)
{
if (Path.IsPathFullyQualified(fullPath))
{
Console.WriteLine($"FOUND: {fullPath}");
}
else
{
Console.WriteLine($"FOUND w/ WARNING: non-full path, {fullPath}");
}
}
else
{
Console.Error.WriteLine("NOT FOUND");
return 1;
}
}
catch (Exception e)
{
Console.Error.WriteLine($"FAILED, Exception: {e}");
return 2;
}
}
return 0;
}
static int TestUnicodeFile()
{
Console.WriteLine("Searching for an (artificial) executable with a unicode name:");
const string gitExec = "git";
const string unicodeExec = "まじかよ。.exe";
if (File.Exists(unicodeExec))
{
File.Delete(unicodeExec);
}
string unicodeExecPath = Path.Join(AppContext.BaseDirectory, unicodeExec);
string? gitFullPath = FindExecutable.FullPath(gitExec);
if (gitFullPath == null)
{
Console.WriteLine("Failed to find git exec as test subject");
return 5;
}
File.Copy(gitFullPath, unicodeExecPath);
if (!OperatingSystem.IsWindows())
{
File.SetUnixFileMode(unicodeExecPath, File.GetUnixFileMode(gitFullPath));
}
if (!FindExecutable.IsExecutable(unicodeExecPath))
{
Console.WriteLine($"Failed to setup the unicode executable file name: {unicodeExecPath}");
return 3;
}
string? findIt = FindExecutable.FullPath(unicodeExec, includeCurrentDirectory: true);
if (string.IsNullOrEmpty(findIt))
{
Console.WriteLine($"Failed to find the unicode executable file name: {unicodeExec}");
return 4;
}
Console.WriteLine($"FOUND: {findIt}");
return 0;
}
}
}