@@ -40,7 +40,19 @@ describe('Utils tests', () => {
4040 expect ( await utils . parseVersion ( '7' ) ) . toBe ( '7.0' ) ;
4141 expect ( await utils . parseVersion ( '7.4' ) ) . toBe ( '7.4' ) ;
4242 expect ( await utils . parseVersion ( '5.x' ) ) . toBe ( '5.6' ) ;
43- expect ( await utils . parseVersion ( '4.x' ) ) . toBe ( undefined ) ;
43+ expect ( await utils . parseVersion ( 'pre' ) ) . toBe ( 'pre' ) ;
44+ expect ( await utils . parseVersion ( 'pre-installed' ) ) . toBe ( 'pre' ) ;
45+ await expect ( utils . parseVersion ( '4.x' ) ) . rejects . toThrow (
46+ 'Invalid PHP version: 4.x'
47+ ) ;
48+ await expect ( utils . parseVersion ( 'foo' ) ) . rejects . toThrow (
49+ 'Invalid PHP version:'
50+ ) ;
51+
52+ fetchSpy . mockResolvedValue ( { data : '{ "latest": "8.1.0" }' } ) ;
53+ await expect ( utils . parseVersion ( 'latest' ) ) . rejects . toThrow (
54+ 'Invalid PHP version in manifest:'
55+ ) ;
4456
4557 fetchSpy . mockReset ( ) ;
4658 fetchSpy . mockResolvedValueOnce ( { } ) . mockResolvedValueOnce ( { } ) ;
@@ -56,6 +68,12 @@ describe('Utils tests', () => {
5668 expect ( await utils . parseIniFile ( 'none' ) ) . toBe ( 'none' ) ;
5769 expect ( await utils . parseIniFile ( 'php.ini-production' ) ) . toBe ( 'production' ) ;
5870 expect ( await utils . parseIniFile ( 'php.ini-development' ) ) . toBe ( 'development' ) ;
71+ expect ( await utils . parseIniFile ( '/etc/php.ini-production' ) ) . toBe (
72+ 'production'
73+ ) ;
74+ expect ( await utils . parseIniFile ( '/a-b/php.ini-development' ) ) . toBe (
75+ 'development'
76+ ) ;
5977 expect ( await utils . parseIniFile ( 'invalid' ) ) . toBe ( 'production' ) ;
6078 } ) ;
6179
@@ -91,6 +109,22 @@ describe('Utils tests', () => {
91109 ) . toEqual ( [ 'apcu' , 'mbstring' , 'pdo_pgsql' , 'posix' , 'session' ] ) ;
92110 } ) ;
93111
112+ it ( 'checking shell helpers' , ( ) => {
113+ expect ( utils . escapeForShell ( 'a$b`c\\d"e' , 'linux' ) ) . toBe (
114+ 'a\\$b\\`c\\\\d\\"e'
115+ ) ;
116+ expect ( utils . escapeForShell ( 'a$b`c"d' , 'win32' ) ) . toBe ( 'a`$b``c`"d' ) ;
117+ expect ( utils . safeArg ( 'vendor-pkg/repo@v1.0.0' , 'linux' ) ) . toBe (
118+ 'vendor-pkg/repo@v1.0.0'
119+ ) ;
120+ expect ( utils . safeArg ( 'phpcs:>=3.0' , 'linux' ) ) . toBe ( '"phpcs:>=3.0"' ) ;
121+ expect ( utils . safeArg ( 'foo$bar' , 'win32' ) ) . toBe ( '"foo`$bar"' ) ;
122+ expect ( utils . sanitizeShellInput ( 'foo;$(`ls`)bar' ) ) . toBe ( 'foolsbar' ) ;
123+ expect ( utils . sanitizeShellInput ( 'vendor/foo:1.*' , true ) ) . toBe (
124+ 'vendor/foo:1.'
125+ ) ;
126+ } ) ;
127+
94128 it ( 'checking INIArray' , async ( ) => {
95129 expect ( await utils . CSVArray ( 'a=1, b=2, c=3' ) ) . toEqual ( [
96130 'a=1' ,
@@ -282,6 +316,9 @@ describe('Utils tests', () => {
282316 process . env [ 'php-version' ] = '8.2' ;
283317 expect ( await utils . readPHPVersion ( ) ) . toBe ( '8.2' ) ;
284318
319+ process . env [ 'php-version' ] = 'pre-installed' ;
320+ expect ( await utils . readPHPVersion ( ) ) . toBe ( 'pre-installed' ) ;
321+
285322 delete process . env [ 'php-version-file' ] ;
286323 delete process . env [ 'php-version' ] ;
287324
@@ -291,7 +328,7 @@ describe('Utils tests', () => {
291328
292329 existsSync . mockReturnValue ( true ) ;
293330 readFileSync . mockReturnValue ( 'setup-php' ) ;
294- expect ( await utils . readPHPVersion ( ) ) . toBe ( 'setup-php ') ;
331+ await expect ( utils . readPHPVersion ( ) ) . rejects . toThrow ( 'Invalid PHP version ') ;
295332
296333 existsSync . mockReturnValueOnce ( false ) . mockReturnValueOnce ( true ) ;
297334 readFileSync . mockReturnValue (
@@ -312,6 +349,37 @@ describe('Utils tests', () => {
312349 readFileSync . mockClear ( ) ;
313350 } ) ;
314351
352+ it ( 'readPHPVersion rejects unsupported values from each source' , async ( ) => {
353+ const existsSync = jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ) ;
354+ const readFileSync = jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ) ;
355+
356+ process . env [ 'php-version' ] = 'bogus' ;
357+ await expect ( utils . readPHPVersion ( ) ) . rejects . toThrow ( 'php-version input' ) ;
358+ delete process . env [ 'php-version' ] ;
359+
360+ existsSync . mockReturnValue ( true ) ;
361+ readFileSync . mockReturnValue ( 'bogus' ) ;
362+ await expect ( utils . readPHPVersion ( ) ) . rejects . toThrow ( '.php-version' ) ;
363+
364+ existsSync . mockReturnValueOnce ( false ) . mockReturnValueOnce ( true ) ;
365+ readFileSync . mockReturnValue ( '{"platform-overrides":{"php":"bogus"}}' ) ;
366+ await expect ( utils . readPHPVersion ( ) ) . rejects . toThrow (
367+ 'composer.lock platform-overrides.php'
368+ ) ;
369+
370+ existsSync
371+ . mockReturnValueOnce ( false )
372+ . mockReturnValueOnce ( false )
373+ . mockReturnValueOnce ( true ) ;
374+ readFileSync . mockReturnValue ( '{"config":{"platform":{"php":"bogus"}}}' ) ;
375+ await expect ( utils . readPHPVersion ( ) ) . rejects . toThrow (
376+ 'composer.json config.platform.php'
377+ ) ;
378+
379+ existsSync . mockClear ( ) ;
380+ readFileSync . mockClear ( ) ;
381+ } ) ;
382+
315383 it ( 'checking setVariable' , async ( ) => {
316384 let script : string = await utils . setVariable ( 'var' , 'command' , 'linux' ) ;
317385 expect ( script ) . toEqual ( '\nvar="$(command)"\n' ) ;
0 commit comments