|
1 | | -// |
2 | | -// Run Docker/Linux test with: |
3 | | -// |
4 | | -// docker build -t sgr/findexecutable . |
5 | | -// docker run --rm sgr/findexecutable |
6 | | -// |
7 | | - |
8 | | -using System; |
9 | | -using System.IO; |
10 | | - |
11 | | -namespace FindExecutable |
12 | | -{ |
13 | | - internal class Program |
14 | | - { |
15 | | - static int Main(string[] args) |
16 | | - { |
17 | | - int retval = 0; |
18 | | - |
19 | | - string[] executables = new string[] { |
20 | | - "git.exe", |
21 | | - "git" |
22 | | - }; |
23 | | - |
24 | | - foreach (string executable in executables) |
25 | | - { |
26 | | - try |
27 | | - { |
28 | | - Console.WriteLine($"Searching for: {executable}"); |
29 | | - |
30 | | - string? fullPath = FindExecutable.FullPath(executable); |
31 | | - |
32 | | - if (fullPath != null) |
33 | | - { |
34 | | - if (Path.IsPathFullyQualified(fullPath)) |
35 | | - { |
36 | | - Console.WriteLine($"FOUND: {fullPath}"); |
37 | | - } |
38 | | - else |
39 | | - { |
40 | | - Console.WriteLine($"FOUND w/ WARNING: non-full path, {fullPath}"); |
41 | | - } |
42 | | - } |
43 | | - else |
44 | | - { |
45 | | - Console.Error.WriteLine("NOT FOUND"); |
46 | | - retval = 1; |
47 | | - } |
48 | | - } |
49 | | - catch (Exception e) |
50 | | - { |
51 | | - Console.Error.WriteLine($"FAILED, Exception: {e}"); |
52 | | - retval = 2; |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - Console.WriteLine("done."); |
| 1 | +// |
| 2 | +// Run Docker/Linux test with: |
| 3 | +// |
| 4 | +// docker build -t sgr/findexecutable . |
| 5 | +// docker run --rm sgr/findexecutable |
| 6 | +// |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.IO; |
| 10 | +using System.Reflection; |
| 11 | + |
| 12 | +namespace FindExecutable |
| 13 | +{ |
| 14 | + internal class Program |
| 15 | + { |
| 16 | + static int Main(string[] args) |
| 17 | + { |
| 18 | + Console.OutputEncoding = System.Text.Encoding.UTF8; |
| 19 | + int retval = 0; |
| 20 | + |
| 21 | + retval = Math.Max(retval, TestSearchGit()); |
| 22 | + |
| 23 | + retval = Math.Max(retval, TestUnicodeFile()); |
| 24 | + |
| 25 | + Console.WriteLine("done."); |
57 | 26 | if (retval != 0) |
58 | 27 | { |
59 | 28 | Console.WriteLine($"exit code: {retval}"); |
60 | | - } |
61 | | - return retval; |
62 | | - } |
63 | | - } |
64 | | -} |
| 29 | + } |
| 30 | + return retval; |
| 31 | + } |
| 32 | + |
| 33 | + static int TestSearchGit() |
| 34 | + { |
| 35 | + string[] executables = [ |
| 36 | + "git.exe", |
| 37 | + "git" |
| 38 | + ]; |
| 39 | + |
| 40 | + foreach (string executable in executables) |
| 41 | + { |
| 42 | + try |
| 43 | + { |
| 44 | + Console.WriteLine($"Searching for: {executable}"); |
| 45 | + |
| 46 | + string? fullPath = FindExecutable.FullPath(executable); |
| 47 | + |
| 48 | + if (fullPath != null) |
| 49 | + { |
| 50 | + if (Path.IsPathFullyQualified(fullPath)) |
| 51 | + { |
| 52 | + Console.WriteLine($"FOUND: {fullPath}"); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + Console.WriteLine($"FOUND w/ WARNING: non-full path, {fullPath}"); |
| 57 | + } |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + Console.Error.WriteLine("NOT FOUND"); |
| 62 | + return 1; |
| 63 | + } |
| 64 | + } |
| 65 | + catch (Exception e) |
| 66 | + { |
| 67 | + Console.Error.WriteLine($"FAILED, Exception: {e}"); |
| 68 | + return 2; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + static int TestUnicodeFile() |
| 76 | + { |
| 77 | + Console.WriteLine("Searching for an (artificial) executable with a unicode name:"); |
| 78 | + |
| 79 | + const string gitExec = "git"; |
| 80 | + const string unicodeExec = "まじかよ。.exe"; |
| 81 | + |
| 82 | + if (File.Exists(unicodeExec)) |
| 83 | + { |
| 84 | + File.Delete(unicodeExec); |
| 85 | + } |
| 86 | + |
| 87 | + string unicodeExecPath = Path.Join(AppContext.BaseDirectory, unicodeExec); |
| 88 | + string? gitFullPath = FindExecutable.FullPath(gitExec); |
| 89 | + |
| 90 | + if (gitFullPath == null) |
| 91 | + { |
| 92 | + Console.WriteLine("Failed to find git exec as test subject"); |
| 93 | + return 5; |
| 94 | + } |
| 95 | + |
| 96 | + File.Copy(gitFullPath, unicodeExecPath); |
| 97 | + |
| 98 | + if (!OperatingSystem.IsWindows()) |
| 99 | + { |
| 100 | + File.SetUnixFileMode(unicodeExecPath, File.GetUnixFileMode(gitFullPath)); |
| 101 | + } |
| 102 | + |
| 103 | + if (!FindExecutable.IsExecutable(unicodeExecPath)) |
| 104 | + { |
| 105 | + Console.WriteLine($"Failed to setup the unicode executable file name: {unicodeExecPath}"); |
| 106 | + return 3; |
| 107 | + } |
| 108 | + |
| 109 | + string? findIt = FindExecutable.FullPath(unicodeExec, includeCurrentDirectory: true); |
| 110 | + if (string.IsNullOrEmpty(findIt)) |
| 111 | + { |
| 112 | + Console.WriteLine($"Failed to find the unicode executable file name: {unicodeExec}"); |
| 113 | + return 4; |
| 114 | + } |
| 115 | + |
| 116 | + Console.WriteLine($"FOUND: {findIt}"); |
| 117 | + |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | +} |
0 commit comments