mirror of
				https://github.com/docker/login-action.git
				synced 2025-11-01 02:30:10 +08:00 
			
		
		
		
	test main
This commit is contained in:
		
							parent
							
								
									f3c2e45637
								
							
						
					
					
						commit
						62670df239
					
				
							
								
								
									
										76
									
								
								__tests__/main.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								__tests__/main.test.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,76 @@ | ||||
| import osm = require('os'); | ||||
| 
 | ||||
| import {run} from '../src/main'; | ||||
| import * as docker from '../src/docker'; | ||||
| import * as stateHelper from '../src/state-helper'; | ||||
| 
 | ||||
| import * as core from '@actions/core'; | ||||
| 
 | ||||
| test('errors when not run on linux platform', async () => { | ||||
|   const platSpy = jest.spyOn(osm, 'platform'); | ||||
|   platSpy.mockImplementation(() => 'netbsd'); | ||||
| 
 | ||||
|   const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed'); | ||||
| 
 | ||||
|   await run(); | ||||
| 
 | ||||
|   expect(coreSpy).toHaveBeenCalledWith('Only supported on linux platform'); | ||||
| }); | ||||
| 
 | ||||
| test('errors without password', async () => { | ||||
|   const platSpy = jest.spyOn(osm, 'platform'); | ||||
|   platSpy.mockImplementation(() => 'linux'); | ||||
| 
 | ||||
|   const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed'); | ||||
| 
 | ||||
|   await run(); | ||||
| 
 | ||||
|   expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: password'); | ||||
| }); | ||||
| 
 | ||||
| test('successful with only password', async () => { | ||||
|     const platSpy = jest.spyOn(osm, 'platform'); | ||||
|     platSpy.mockImplementation(() => 'linux'); | ||||
|    | ||||
|     const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry'); | ||||
|     const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout'); | ||||
|     const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login'); | ||||
|     dockerSpy.mockImplementation(() => {}); | ||||
|    | ||||
|     const password: string = 'groundcontrol'; | ||||
|     process.env[`INPUT_PASSWORD`] = password; | ||||
| 
 | ||||
|     await run(); | ||||
| 
 | ||||
|     expect(setRegistrySpy).toHaveBeenCalledWith(''); | ||||
|     expect(setLogoutSpy).toHaveBeenCalledWith(''); | ||||
|     expect(dockerSpy).toHaveBeenCalledWith('', '', password); | ||||
| }); | ||||
| 
 | ||||
| test('calls docker login', async () => { | ||||
|   const platSpy = jest.spyOn(osm, 'platform'); | ||||
|   platSpy.mockImplementation(() => 'linux'); | ||||
| 
 | ||||
|   const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry'); | ||||
|   const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout'); | ||||
|   const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login'); | ||||
|   dockerSpy.mockImplementation(() => {}); | ||||
| 
 | ||||
|   const username: string = 'dbowie'; | ||||
|   process.env[`INPUT_USERNAME`] = username; | ||||
| 
 | ||||
|   const password: string = 'groundcontrol'; | ||||
|   process.env[`INPUT_PASSWORD`] = password; | ||||
| 
 | ||||
|   const registry: string = 'https://ghcr.io'; | ||||
|   process.env[`INPUT_REGISTRY`] = registry; | ||||
| 
 | ||||
|   const logout: string = 'true'; | ||||
|   process.env['INPUT_LOGOUT'] = logout | ||||
| 
 | ||||
|   await run(); | ||||
| 
 | ||||
|   expect(setRegistrySpy).toHaveBeenCalledWith(registry); | ||||
|   expect(setLogoutSpy).toHaveBeenCalledWith(logout); | ||||
|   expect(dockerSpy).toHaveBeenCalledWith(registry, username, password); | ||||
| }); | ||||
| @ -4,11 +4,10 @@ import {getInputs, Inputs} from './context'; | ||||
| import * as docker from './docker'; | ||||
| import * as stateHelper from './state-helper'; | ||||
| 
 | ||||
| async function run(): Promise<void> { | ||||
| export async function run(): Promise<void> { | ||||
|   try { | ||||
|     if (os.platform() !== 'linux') { | ||||
|       core.setFailed('Only supported on linux platform'); | ||||
|       return; | ||||
|       throw new Error('Only supported on linux platform'); | ||||
|     } | ||||
| 
 | ||||
|     const {registry, username, password, logout} = getInputs(); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user