fix: allow plugins to set environment variables to be used by other plugins
This commit is contained in:
		
							parent
							
								
									8ce2d6e834
								
							
						
					
					
						commit
						68f7e928f9
					
				| @ -6,7 +6,7 @@ const PLUGINS_DEFINITIONS = require('../definitions/plugins'); | |||||||
| const {loadPlugin, parseConfig} = require('./utils'); | const {loadPlugin, parseConfig} = require('./utils'); | ||||||
| 
 | 
 | ||||||
| module.exports = (context, type, pluginOpt, pluginsPath) => { | module.exports = (context, type, pluginOpt, pluginsPath) => { | ||||||
|   const {stdout, stderr, options, logger} = context; |   const {stdout, stderr, options, logger, env} = context; | ||||||
|   if (!pluginOpt) { |   if (!pluginOpt) { | ||||||
|     return noop; |     return noop; | ||||||
|   } |   } | ||||||
| @ -32,10 +32,11 @@ module.exports = (context, type, pluginOpt, pluginsPath) => { | |||||||
|       if (!input.options.dryRun || dryRun) { |       if (!input.options.dryRun || dryRun) { | ||||||
|         logger.log(`Start step "${type}" of plugin "${pluginName}"`); |         logger.log(`Start step "${type}" of plugin "${pluginName}"`); | ||||||
|         const result = await func({ |         const result = await func({ | ||||||
|           ...cloneDeep(omit(input, ['stdout', 'stderr', 'logger'])), |           ...cloneDeep(omit(input, ['stdout', 'stderr', 'logger', 'env'])), | ||||||
|           stdout, |           stdout, | ||||||
|           stderr, |           stderr, | ||||||
|           logger: logger.scope(logger.scopeName, pluginName), |           logger: logger.scope(logger.scopeName, pluginName), | ||||||
|  |           env, | ||||||
|         }); |         }); | ||||||
|         if (outputValidator && !outputValidator(result)) { |         if (outputValidator && !outputValidator(result)) { | ||||||
|           throw getError(`E${type.toUpperCase()}OUTPUT`, {result, pluginName}); |           throw getError(`E${type.toUpperCase()}OUTPUT`, {result, pluginName}); | ||||||
|  | |||||||
| @ -1941,3 +1941,51 @@ test('Get all commits including the ones not in the shallow clone', async t => { | |||||||
| 
 | 
 | ||||||
|   t.is(analyzeCommits.args[0][1].commits.length, 3); |   t.is(analyzeCommits.args[0][1].commits.length, 3); | ||||||
| }); | }); | ||||||
|  | 
 | ||||||
|  | test('Allow plugins to set environment variables', async t => { | ||||||
|  |   const {cwd, repositoryUrl} = await gitRepo(true); | ||||||
|  |   await gitCommits(['First'], {cwd}); | ||||||
|  |   await gitTagVersion('v1.0.0', undefined, {cwd}); | ||||||
|  |   await gitCommits(['Second'], {cwd}); | ||||||
|  |   await gitPush(repositoryUrl, 'master', {cwd}); | ||||||
|  | 
 | ||||||
|  |   const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'}; | ||||||
|  |   const verifyConditions = stub().resolves(); | ||||||
|  |   const analyzeCommits = stub().resolves(nextRelease.type); | ||||||
|  |   const verifyRelease = stub().resolves(); | ||||||
|  |   const generateNotes = stub().resolves(); | ||||||
|  |   const prepare = stub().resolves(); | ||||||
|  |   const publish1 = async (_, context) => { | ||||||
|  |     context.env.TEST_VAR = 'test-value'; | ||||||
|  |   }; | ||||||
|  | 
 | ||||||
|  |   const publish2 = stub().resolves(); | ||||||
|  |   const success = stub().resolves(); | ||||||
|  |   const env = {...process.env}; | ||||||
|  |   const config = {branch: 'master', repositoryUrl}; | ||||||
|  |   const options = { | ||||||
|  |     ...config, | ||||||
|  |     plugins: false, | ||||||
|  |     verifyConditions, | ||||||
|  |     analyzeCommits, | ||||||
|  |     verifyRelease, | ||||||
|  |     generateNotes, | ||||||
|  |     prepare, | ||||||
|  |     publish: [publish1, publish2], | ||||||
|  |     success, | ||||||
|  |   }; | ||||||
|  | 
 | ||||||
|  |   const semanticRelease = requireNoCache('..', { | ||||||
|  |     './lib/get-logger': () => t.context.logger, | ||||||
|  |     'env-ci': () => ({isCi: true, branch: 'master', isPr: false}), | ||||||
|  |   }); | ||||||
|  |   await semanticRelease(options, { | ||||||
|  |     cwd, | ||||||
|  |     env, | ||||||
|  |     stdout: new WritableStreamBuffer(), | ||||||
|  |     stderr: new WritableStreamBuffer(), | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   t.is(publish2.args[0][1].env.TEST_VAR, 'test-value'); | ||||||
|  |   t.is(env.TEST_VAR, 'test-value'); | ||||||
|  | }); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user