Skip to content

Conversation

@raymondfeng
Copy link
Contributor

@raymondfeng raymondfeng commented Jun 16, 2020

Extracted from #5744 for ease of review.

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for extracting this change in to a smaller pull request, it's much easier to review 👏

The changes are reasonable, I'd like to discuss where to put the ENV-based behavior. <-- that was for #5744 🙈

Please get feedback from code owners (@jannyHou) too.

const appJsFile = resolve(__dirname, '../fixtures/application.js');
let appJs = fs.readFileSync(appJsFile, 'utf-8');
// Adjust the relative path for `import`
appJs = appJs.replace('../..', '../../..');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this logic into sandbox.copyFile please? IIRC, copyFile is already detecting JS files and updating sourcemap URL to accommodate the new location, it would be great if we could do the same for import paths.

If it's too work to detect which paths to update, then perhaps copyFile can accept a new options to specify the mapping? Example usage:

await sandbox.copyFile(
  resolve(__dirname, '../fixtures/application.js'),
  'dist/application.js',
  {
    transform: text => text.replace('../..', '../../..'),
  },
});

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should defer it. The current copyFile does not read the source file and we don't even know if the source is a text or binary file.

Copy link
Member

@bajtos bajtos Jun 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context, here is the current copyFile implementation, you are right that we are not reading the source file.

https://github.com/strongloop/loopback-next/blob/862072b82ad88ec4b248e0b243dda58b53640f94/packages/testlab/src/test-sandbox.ts#L135-L146

I have two concerns about your current version:

(1)
The code is a bit involved and at different level of abstraction when compared the surrounding lines.

I'd like to read getApp as a sequence of instructions for copying files, I don't want to be distracted with implementation details of transforming source file content into target.

await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js'), {
  // Adjust the relative path for `import`
  transform: text => text.replace('../..', '../../..'),
});
await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json'));

(2)
The code modifying application.js is duplicated in two test suites. These two places can easily get out of sync and it's likely additional copies will be introduced in the future as more test files are added.

Can you at least extract the code dealing with application.js into a shared test helper please, to avoid duplication?

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not entirely happy with the proposed version, but I guess we can improve it later.

const appJsFile = resolve(__dirname, '../fixtures/application.js');
let appJs = fs.readFileSync(appJsFile, 'utf-8');
// Adjust the relative path for `import`
appJs = appJs.replace('../..', '../../..');
Copy link
Member

@bajtos bajtos Jun 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context, here is the current copyFile implementation, you are right that we are not reading the source file.

https://github.com/strongloop/loopback-next/blob/862072b82ad88ec4b248e0b243dda58b53640f94/packages/testlab/src/test-sandbox.ts#L135-L146

I have two concerns about your current version:

(1)
The code is a bit involved and at different level of abstraction when compared the surrounding lines.

I'd like to read getApp as a sequence of instructions for copying files, I don't want to be distracted with implementation details of transforming source file content into target.

await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js'), {
  // Adjust the relative path for `import`
  transform: text => text.replace('../..', '../../..'),
});
await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json'));

(2)
The code modifying application.js is duplicated in two test suites. These two places can easily get out of sync and it's likely additional copies will be introduced in the future as more test files are added.

Can you at least extract the code dealing with application.js into a shared test helper please, to avoid duplication?

} catch (err) {
console.log(`Stopping the app threw an error: ${err}`);
}
await app?.stop();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the state first, for consistency with other stopApp implementations?

Suggested change
await app?.stop();
if (app?.state === 'started') await app?.stop();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, app.start() is called.

@bajtos
Copy link
Member

bajtos commented Jun 26, 2020

@raymondfeng FYI, it looks like these improvements are not enough to fix all booter tests when executed in parallel.

Can you please rebase your patch on top of the latest master and run npm t -- -p to check yourself?

Here are the test failures I am seeing: https://gist.github.com/bajtos/c2f45df9c41384c710d3712186910661

const appJsFile = resolve(__dirname, '../fixtures/application.js');
let appJs = fs.readFileSync(appJsFile, 'utf-8');
// Adjust the relative path for `import`
appJs = appJs.replace('../..', '../../..');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, we are trying to fix the following line:

https://github.com/strongloop/loopback-next/blob/4e5f721ca666ab5047f4c36fbcb36a39b76966bb/packages/boot/src/__tests__/fixtures/application.ts#L10

An idea to consider: instead of changing the import/require path in the application file copied to sandbox, can we put a short index.js file into the directory where ../.. points to, to make require('../..') work again?

// index.js
module.exports = require('../');

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 prefer to use transform to make it clear - having a magic index.js makes it difficult to understand.

@raymondfeng raymondfeng force-pushed the unique-sandbox-for-tests branch from 1bc25f5 to 6ad5c9e Compare June 26, 2020 17:38
@raymondfeng raymondfeng requested a review from agnes512 as a code owner June 26, 2020 17:38
@raymondfeng
Copy link
Contributor Author

@bajtos I added transform option for TestSandbox.copyFile.

@raymondfeng
Copy link
Contributor Author

@raymondfeng FYI, it looks like these improvements are not enough to fix all booter tests when executed in parallel.
Can you please rebase your patch on top of the latest master and run npm t -- -p to check yourself?
Here are the test failures I am seeing: https://gist.github.com/bajtos/c2f45df9c41384c710d3712186910661

I cannot reproduce the issue. With this PR, npm t -- -p works for me.

@raymondfeng raymondfeng merged commit 225411c into master Jun 26, 2020
@raymondfeng raymondfeng deleted the unique-sandbox-for-tests branch June 26, 2020 17:53
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.

3 participants