@@ -50,6 +50,164 @@ public async Task TestWhichFunctionForNonExistingFile()
5050 Assert . Equal ( "" , result . Item2 ) ;
5151 }
5252
53+ [ Fact ]
54+ public void TestWhichFunctionForDirectPath ( )
55+ {
56+ string tempDirectory = CreateTemporaryDirectory ( ) ;
57+ string commandPath = Path . Combine (
58+ tempDirectory ,
59+ OperatingSystem . IsWindows ( ) ? "unigetui-direct-path-test.cmd" : "unigetui-direct-path-test"
60+ ) ;
61+
62+ try
63+ {
64+ CreateCommandFile ( commandPath ) ;
65+
66+ Tuple < bool , string > result = CoreTools . Which ( commandPath ) ;
67+
68+ Assert . True ( result . Item1 ) ;
69+ Assert . Equal ( commandPath , result . Item2 ) ;
70+ }
71+ finally
72+ {
73+ Directory . Delete ( tempDirectory , true ) ;
74+ }
75+ }
76+
77+ [ Fact ]
78+ public void TestWhichMultipleRespectsProcessPathOrder ( )
79+ {
80+ string tempDirectory1 = CreateTemporaryDirectory ( ) ;
81+ string tempDirectory2 = CreateTemporaryDirectory ( ) ;
82+ string commandName = OperatingSystem . IsWindows ( )
83+ ? "unigetui-path-order-test.exe"
84+ : "unigetui-path-order-test" ;
85+ string commandPath1 = Path . Combine ( tempDirectory1 , commandName ) ;
86+ string commandPath2 = Path . Combine ( tempDirectory2 , commandName ) ;
87+ string ? oldPath = Environment . GetEnvironmentVariable ( "PATH" , EnvironmentVariableTarget . Process ) ;
88+
89+ try
90+ {
91+ CreateCommandFile ( commandPath1 ) ;
92+ CreateCommandFile ( commandPath2 ) ;
93+ Environment . SetEnvironmentVariable (
94+ "PATH" ,
95+ string . Join ( Path . PathSeparator , tempDirectory1 , tempDirectory2 ) ,
96+ EnvironmentVariableTarget . Process
97+ ) ;
98+
99+ List < string > result = CoreTools . WhichMultiple ( commandName ) ;
100+
101+ Assert . Equal ( [ commandPath1 , commandPath2 ] , result ) ;
102+ }
103+ finally
104+ {
105+ Environment . SetEnvironmentVariable (
106+ "PATH" ,
107+ oldPath ,
108+ EnvironmentVariableTarget . Process
109+ ) ;
110+ Directory . Delete ( tempDirectory1 , true ) ;
111+ Directory . Delete ( tempDirectory2 , true ) ;
112+ }
113+ }
114+
115+ [ Fact ]
116+ public void TestWhichFunctionResolvesPathextOnWindows ( )
117+ {
118+ if ( ! OperatingSystem . IsWindows ( ) )
119+ {
120+ return ;
121+ }
122+
123+ string tempDirectory = CreateTemporaryDirectory ( ) ;
124+ string commandPath = Path . Combine ( tempDirectory , "unigetui-pathext-test.exe" ) ;
125+ string ? oldPath = Environment . GetEnvironmentVariable ( "PATH" , EnvironmentVariableTarget . Process ) ;
126+ string ? oldPathExt = Environment . GetEnvironmentVariable (
127+ "PATHEXT" ,
128+ EnvironmentVariableTarget . Process
129+ ) ;
130+
131+ try
132+ {
133+ CreateCommandFile ( commandPath ) ;
134+ Environment . SetEnvironmentVariable (
135+ "PATH" ,
136+ tempDirectory ,
137+ EnvironmentVariableTarget . Process
138+ ) ;
139+ Environment . SetEnvironmentVariable (
140+ "PATHEXT" ,
141+ ".EXE;.CMD" ,
142+ EnvironmentVariableTarget . Process
143+ ) ;
144+
145+ Tuple < bool , string > result = CoreTools . Which ( "unigetui-pathext-test" ) ;
146+
147+ Assert . True ( result . Item1 ) ;
148+ Assert . Equal ( commandPath , result . Item2 , StringComparer . OrdinalIgnoreCase ) ;
149+ }
150+ finally
151+ {
152+ Environment . SetEnvironmentVariable (
153+ "PATH" ,
154+ oldPath ,
155+ EnvironmentVariableTarget . Process
156+ ) ;
157+ Environment . SetEnvironmentVariable (
158+ "PATHEXT" ,
159+ oldPathExt ,
160+ EnvironmentVariableTarget . Process
161+ ) ;
162+ Directory . Delete ( tempDirectory , true ) ;
163+ }
164+ }
165+
166+ [ Fact ]
167+ public void TestWhichFunctionRequiresExecutableBitOnUnix ( )
168+ {
169+ if ( OperatingSystem . IsWindows ( ) )
170+ {
171+ return ;
172+ }
173+
174+ string tempDirectory = CreateTemporaryDirectory ( ) ;
175+ string commandName = "unigetui-unix-executable-bit-test" ;
176+ string commandPath = Path . Combine ( tempDirectory , commandName ) ;
177+ string ? oldPath = Environment . GetEnvironmentVariable ( "PATH" , EnvironmentVariableTarget . Process ) ;
178+
179+ try
180+ {
181+ File . WriteAllText ( commandPath , string . Empty ) ;
182+ File . SetUnixFileMode (
183+ commandPath ,
184+ UnixFileMode . UserRead
185+ | UnixFileMode . UserWrite
186+ | UnixFileMode . GroupRead
187+ | UnixFileMode . OtherRead
188+ ) ;
189+ Environment . SetEnvironmentVariable (
190+ "PATH" ,
191+ tempDirectory ,
192+ EnvironmentVariableTarget . Process
193+ ) ;
194+
195+ Tuple < bool , string > result = CoreTools . Which ( commandName ) ;
196+
197+ Assert . False ( result . Item1 ) ;
198+ Assert . Equal ( "" , result . Item2 ) ;
199+ }
200+ finally
201+ {
202+ Environment . SetEnvironmentVariable (
203+ "PATH" ,
204+ oldPath ,
205+ EnvironmentVariableTarget . Process
206+ ) ;
207+ Directory . Delete ( tempDirectory , true ) ;
208+ }
209+ }
210+
53211 [ Theory ]
54212 [ InlineData ( "7zip19.00-helpEr" , "7zip19.00 HelpEr" ) ]
55213 [ InlineData ( "packagename" , "Packagename" ) ]
@@ -242,5 +400,31 @@ public void TestFormatSize(long size, int decPlaces, string expected)
242400 {
243401 Assert . Equal ( CoreTools . FormatAsSize ( size , decPlaces ) . Replace ( ',' , '.' ) , expected ) ;
244402 }
403+
404+ private static string CreateTemporaryDirectory ( )
405+ {
406+ string path = Path . Combine ( Path . GetTempPath ( ) , $ "UniGetUI-ToolsTests-{ Guid . NewGuid ( ) : N} ") ;
407+ Directory . CreateDirectory ( path ) ;
408+ return path ;
409+ }
410+
411+ private static void CreateCommandFile ( string path )
412+ {
413+ File . WriteAllText ( path , string . Empty ) ;
414+
415+ if ( ! OperatingSystem . IsWindows ( ) )
416+ {
417+ File . SetUnixFileMode (
418+ path ,
419+ UnixFileMode . UserRead
420+ | UnixFileMode . UserWrite
421+ | UnixFileMode . UserExecute
422+ | UnixFileMode . GroupRead
423+ | UnixFileMode . GroupExecute
424+ | UnixFileMode . OtherRead
425+ | UnixFileMode . OtherExecute
426+ ) ;
427+ }
428+ }
245429 }
246430}
0 commit comments