-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: print a development version when using a custom web-ext build #565
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I requested a few changes but there is a bigger problem (as you mentioned on IRC) about NODE_ENV. This variable is defined during npm run build
but the web-ext
version is not generated during npm run build
. Oops, this was an oversight. We need to think of a different approach.
assert.equal(defaultVersionGetter(root), | ||
JSON.parse(pkgData).version); | ||
}); | ||
.then((pkgData) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor problem here: this block should be indented two spaces to show that .then()
is a continuation of the call to readFile()
.
|
||
it('returns git commit information in development', () => { | ||
const commit = `${git.branch()}-${git.long()}`; | ||
assert.equal(defaultVersionGetter(root), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be called using a testProcess
object just like before. Whenever you have a test like this that relies on a specific state of a dependency (i.e. process
) you need to explicitly set up that state, even though it's supposed to be the default state. Something like this:
const testProcess = {
// Empty env to simulate not declaring NODE_ENV.
env: {},
};
defaultVersionGetter(root, testProcess)
localProcess: Object = process | ||
): string { | ||
if (localProcess.env.NODE_ENV === 'production') { | ||
log.debug('Getting the version from package.json'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, the code needs this fix to make the log statements show up in --verbose
mode:
diff --git a/src/program.js b/src/program.js
index eb9e8ae..5bf90ae 100644
--- a/src/program.js
+++ b/src/program.js
@@ -92,8 +92,8 @@ export class Program {
let runCommand = this.commands[cmd];
if (argv.verbose) {
- log.info('Version:', getVersion(absolutePackageDir));
logStream.makeVerbose();
+ log.info('Version:', getVersion(absolutePackageDir));
}
try {
One thing I can think of is to define a magic global at compile time (when new webpack.DefinePlugin({
WEBEXT_BUILD_ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
}) |
it('returns the package version', () => { | ||
let root = path.join(__dirname, '..', '..'); | ||
let root = path.join(__dirname, '..', '..'); | ||
let testProcess = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be shared be a shared variable because it makes the tests unpredictable (say, if they were run in reverse order). I'd say keep it defined as a local constant within each respective test function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work on the tests! I just requested some minor changes and one issue from the last review still needs to be resolved.
@@ -123,11 +125,21 @@ export class Program { | |||
} | |||
} | |||
|
|||
declare var WEBEXT_BUILD_ENV: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth adding a comment above this explaining how this is generated by webpack and that you can see how it's generated in webpack.config.js
.
@@ -166,6 +168,7 @@ describe('program.Program', () => { | |||
|
|||
it('configures the logger when verbose', () => { | |||
const logStream = fake(new ConsoleStream()); | |||
let version = spy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always use const
if the variable does not need to be modified
return JSON.parse(packageData).version; | ||
export function defaultVersionGetter( | ||
absolutePackageDir: string, | ||
localEnv: string = WEBEXT_BUILD_ENV |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is an optional argument, it should be defined as a keyword argument instead. This help makes the calls easier to read and it makes it easy to add more options later on. Example:
export function defaultVersionGetter(
absolutePackageDir: string,
{localEnv: string = WEBEXT_BUILD_ENV} = {}
): string {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I committed in this form, but it breaks the eslint tests. Should i insert inline rule disable comments?
@@ -175,7 +178,7 @@ describe('program.Program', () => { | |||
}); | |||
program.command('thing', 'does a thing', () => {}); | |||
|
|||
return execProgram(program, {logStream}) | |||
return execProgram(program, {getVersion: version, logStream}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like this can just be getVersion: spy()
since the version
variable isn't used elsewhere
return JSON.parse(packageData).version; | ||
export function defaultVersionGetter( | ||
absolutePackageDir: string, | ||
{localEnv: string = WEBEXT_BUILD_ENV} = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry, I gave you the wrong example code. The lint error is correct and it needs to be fixed. The problem is actually an obscure ES6 vs Flow problem so let me offer the solution to help you move forward.
First, define a type for the option object that needs to be passed to defaultVersionGetter()
, like this:
type versionGetterOptions = {
localEnv?: string,
};
Next, fix the definition of defaultVersionGetter
to reference the new type:
export function defaultVersionGetter(
absolutePackageDir: string,
{localEnv = WEBEXT_BUILD_ENV}: versionGetterOptions = {}
): string {
After this change, you'll need to update all the calls to defaultVersionGetter()
. The flow checks in npm start
should help guide you on what to change and where.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated just the test, default argument took care of all other calls. Seems to be finally working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick follow-ups, this looks great. I added some very minor nit-picky comments just for future reference. You don't need to fix these up.
let packageData: any = readFileSync( | ||
path.join(absolutePackageDir, 'package.json')); | ||
return JSON.parse(packageData).version; | ||
//A defintion of type of argument for defaultVersionGetter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for future note: comments typically begin with a space elsewhere in the codebase, like // A definition...
assert.equal(defaultVersionGetter(root), | ||
JSON.parse(pkgData).version); | ||
const testBuildEnv = {localEnv: 'production'}; | ||
assert.equal(defaultVersionGetter(root, testBuildEnv), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could make this a bit more concise like:
assert.equal(defaultVersionGetter(root, {localEnv: 'production'})
Not a big deal though, don't worry about changing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was on the fence about doing that, thanks for clarification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I don't mind fixing these, I've committed them to my branch in case you have time to merge them as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, it's a bit tricky to pick those from a different branch. No worries! They aren't important changes but thanks.
I've added your contribution to our recognition wiki--thanks, and welcome to our project! |
Fixes #481