mirror of
				https://github.com/actions/setup-go.git
				synced 2025-11-04 01:13:43 +00:00 
			
		
		
		
	Add some unit tests fot cache-utils.ts file
This commit is contained in:
		
							parent
							
								
									2e1e7c7d7e
								
							
						
					
					
						commit
						4d62da4173
					
				
							
								
								
									
										106
									
								
								__tests__/cache-utils.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								__tests__/cache-utils.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,106 @@
 | 
				
			|||||||
 | 
					import * as exec from '@actions/exec';
 | 
				
			||||||
 | 
					import * as cacheUtils from '../src/cache-utils';
 | 
				
			||||||
 | 
					import {PackageManagerInfo} from '../src/package-managers';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('getCommandOutput', () => {
 | 
				
			||||||
 | 
					  //Arrange
 | 
				
			||||||
 | 
					  let getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it('should return trimmed stdout in case of successful exit code', async () => {
 | 
				
			||||||
 | 
					    //Arrange
 | 
				
			||||||
 | 
					    const stdoutResult = ' some stdout ';
 | 
				
			||||||
 | 
					    const trimmedStdout = stdoutResult.trim();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getExecOutputSpy.mockImplementation((commandLine: string) => {
 | 
				
			||||||
 | 
					      return new Promise<exec.ExecOutput>(resolve => {
 | 
				
			||||||
 | 
					        resolve({exitCode: 0, stdout: stdoutResult, stderr: ''});
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Act + Assert
 | 
				
			||||||
 | 
					    return cacheUtils
 | 
				
			||||||
 | 
					      .getCommandOutput('some command')
 | 
				
			||||||
 | 
					      .then(data => expect(data).toBe(trimmedStdout));
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it('should return error in case of unsuccessful exit code', async () => {
 | 
				
			||||||
 | 
					    //Arrange
 | 
				
			||||||
 | 
					    const stderrResult = 'Some error message';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getExecOutputSpy.mockImplementation((commandLine: string) => {
 | 
				
			||||||
 | 
					      return new Promise<exec.ExecOutput>(resolve => {
 | 
				
			||||||
 | 
					        resolve({exitCode: 10, stdout: '', stderr: stderrResult});
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Act + Assert
 | 
				
			||||||
 | 
					    expect(async () => {
 | 
				
			||||||
 | 
					      await cacheUtils.getCommandOutput('some command');
 | 
				
			||||||
 | 
					    }).rejects.toThrow();
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('getPackageManagerInfo', () => {
 | 
				
			||||||
 | 
					  it('should return package manager info in case of valid package manager name', async () => {
 | 
				
			||||||
 | 
					    //Arrange
 | 
				
			||||||
 | 
					    const packageManagerName = 'default';
 | 
				
			||||||
 | 
					    const expectedResult = {
 | 
				
			||||||
 | 
					      dependencyFilePattern: 'go.sum',
 | 
				
			||||||
 | 
					      getCacheFolderCommand: 'go env GOMODCACHE'
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Act + Assert
 | 
				
			||||||
 | 
					    return cacheUtils
 | 
				
			||||||
 | 
					      .getPackageManagerInfo(packageManagerName)
 | 
				
			||||||
 | 
					      .then(data => expect(data).toEqual(expectedResult));
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it('should throw the error in case of invalid package manager name', async () => {
 | 
				
			||||||
 | 
					    //Arrange
 | 
				
			||||||
 | 
					    const packageManagerName = 'invalidName';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Act + Assert
 | 
				
			||||||
 | 
					    expect(async () => {
 | 
				
			||||||
 | 
					      await cacheUtils.getPackageManagerInfo(packageManagerName);
 | 
				
			||||||
 | 
					    }).rejects.toThrow();
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('getCacheDirectoryPath', () => {
 | 
				
			||||||
 | 
					  //Arrange
 | 
				
			||||||
 | 
					  let getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const validPackageManager: PackageManagerInfo = {
 | 
				
			||||||
 | 
					    dependencyFilePattern: 'go.sum',
 | 
				
			||||||
 | 
					    getCacheFolderCommand: 'go env GOMODCACHE'
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it('should return path to the cache folder which specified package manager uses', async () => {
 | 
				
			||||||
 | 
					    //Arrange
 | 
				
			||||||
 | 
					    getExecOutputSpy.mockImplementation((commandLine: string) => {
 | 
				
			||||||
 | 
					      return new Promise<exec.ExecOutput>(resolve => {
 | 
				
			||||||
 | 
					        resolve({exitCode: 0, stdout: 'path/to/cache/folder', stderr: ''});
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const expectedResult = 'path/to/cache/folder';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Act + Assert
 | 
				
			||||||
 | 
					    return cacheUtils
 | 
				
			||||||
 | 
					      .getCacheDirectoryPath(validPackageManager)
 | 
				
			||||||
 | 
					      .then(data => expect(data).toEqual(expectedResult));
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it('should throw if the specified package name is invalid', async () => {
 | 
				
			||||||
 | 
					    getExecOutputSpy.mockImplementation((commandLine: string) => {
 | 
				
			||||||
 | 
					      return new Promise<exec.ExecOutput>(resolve => {
 | 
				
			||||||
 | 
					        resolve({exitCode: 10, stdout: '', stderr: 'Error message'});
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Act + Assert
 | 
				
			||||||
 | 
					    expect(async () => {
 | 
				
			||||||
 | 
					      await cacheUtils.getCacheDirectoryPath(validPackageManager);
 | 
				
			||||||
 | 
					    }).rejects.toThrow();
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
import * as exec from '@actions/exec';
 | 
					import * as exec from '@actions/exec';
 | 
				
			||||||
import {supportedPackageManagers, PackageManagerInfo} from './package-managers';
 | 
					import {supportedPackageManagers, PackageManagerInfo} from './package-managers';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const getCommandOutput = async (toolCommand: string) => {
 | 
					export const getCommandOutput = async (toolCommand: string) => {
 | 
				
			||||||
  let {stdout, stderr, exitCode} = await exec.getExecOutput(
 | 
					  let {stdout, stderr, exitCode} = await exec.getExecOutput(
 | 
				
			||||||
    toolCommand,
 | 
					    toolCommand,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user