Skip to content
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

Merged
merged 6 commits into from
Oct 14, 2016

Conversation

saintsebastian
Copy link
Contributor

Fixes #481

@coveralls
Copy link

coveralls commented Oct 10, 2016

Coverage Status

Coverage decreased (-0.3%) to 99.688% when pulling 17957f1 on saintsebastian:fix-481 into 8e4e657 on mozilla:master.

Copy link
Contributor

@kumar303 kumar303 left a 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) => {
Copy link
Contributor

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),
Copy link
Contributor

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');
Copy link
Contributor

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 {

@coveralls
Copy link

coveralls commented Oct 10, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling ee764a6 on saintsebastian:fix-481 into 8e4e657 on mozilla:master.

@kumar303
Copy link
Contributor

kumar303 commented Oct 10, 2016

One thing I can think of is to define a magic global at compile time (when npm run build executes), using the DefinePlugin. You could add this to webpack.config.js:

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 = {
Copy link
Contributor

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.

@coveralls
Copy link

coveralls commented Oct 11, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling c193075 on saintsebastian:fix-481 into 8e4e657 on mozilla:master.

Copy link
Contributor

@kumar303 kumar303 left a 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;
Copy link
Contributor

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();
Copy link
Contributor

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
Copy link
Contributor

@kumar303 kumar303 Oct 11, 2016

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 {

Copy link
Contributor Author

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})
Copy link
Contributor

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} = {}
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@coveralls
Copy link

coveralls commented Oct 14, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling bdd0929 on saintsebastian:fix-481 into 8e4e657 on mozilla:master.

Copy link
Contributor

@kumar303 kumar303 left a 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
Copy link
Contributor

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),
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@kumar303 kumar303 merged commit 1fcb2e2 into mozilla:master Oct 14, 2016
@atsay
Copy link

atsay commented Oct 20, 2016

I've added your contribution to our recognition wiki--thanks, and welcome to our project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants