-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(testlab): update sourceMappingURL when copying a JS file #951
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
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.
Okay, this PR looks simple enough, and I'm okay with the content, but let's timebox it at 30 minutes, or however long the first CI run is. If we can't get it all green by then, let's hang it up for later.
I want to avoid every non-MVP PR that we can.
kjdelisle
left a comment
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.
Actually, given that there are some bits I've overlooked in the TypeDoc (that I'm not sure of) I think this should wait until post-MVP.
packages/testlab/src/test-sandbox.ts
Outdated
| * Copies a file from src to the TestSandbox. `.js.map` files are also copied | ||
| * to the sandbox by default. | ||
| * @param src Absolute path of file to be copied to the TestSandbox | ||
| * @param [dest] Optional. Destination path / filename of the copy operation |
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.
Shouldn't this be @param [string] dest Optional... ?
bajtos
left a comment
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 is great, @virkt25!
Please add a test to verify that copySourceCodeFile does not choke on error when there is no sourcemap file at the source.
packages/testlab/src/test-sandbox.ts
Outdated
| async copySourceCodeFile( | ||
| src: string, | ||
| dest?: string, | ||
| copyMap: boolean = true, |
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.
In general: Boolean arguments are evil, please use an options object {copyMap: boolean} instead.
In this particular use case, I think copyMap option is YAGNI. Since your code is already handling the case where no .js.map file is available (and skip copying), I think this is all we need for now.
| it('copies source file and map to directory', async () => { | ||
| await sandbox.copySourceCodeFile(TEST_FILE_RESOLVED); | ||
| expect(await pathExists(resolve(path, TEST_FILE))).to.be.True(); | ||
| expect(await pathExists(resolve(path, TEST_FILE_MAP))).to.be.True(); |
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.
These assertions will produce error messages that are rather unhelpful ("expected false to be true"). Please create a helper for verifying that a path exists, one that will include the file path in the error message.
For example:
async expectPathExists(filePath: string) {
expect.ok(
await pathExists(filePath),
`Expected "${filePath}" to exist.`);
}
While I agree that we should avoid every non-MVP PR that we can (*), I disagree with the claim that this PR is non-MVP. This particular pull request was created in response to my comment in #858 (comment) which does belong to MVP scope. While @virkt25 could make the changes proposed here in #858 (in which case you probably would not complain @kjdelisle , would you?), I appreciate that he did the right thing and opened a new PR where we can discuss those changes in isolation from the rest of that original PR that's already rather big. My understanding of MVP is to keep the feature scope small, it is not an excuse for writing unclean code and introducing tech debt. (*) I think we should consider an exception to that rule: pull requests improving our dev process (e.g. speeding our build-test cycles) should be allowed. |
|
Some notes for future reference when this PR is worked on. Based on current usage of TestSandbox, the following will be very helpful and make using this utility much easier and cleaner.
|
|
@virkt25 thank you for writing down the information for our future reference!
Wow, I find this rather surprising. Are you saying that after copy+rename, we will end up with |
|
The last line of the original An example last line in a compiled |
159a4bb to
a513ccb
Compare
shimks
left a comment
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.
LGTM. One minor nitpick
| async function compareFiles(path1: string) { | ||
| const file = await readFile(path1, 'utf8'); | ||
| expect(file).to.equal(fileContent); | ||
| async function compareFiles(original: string, copied: 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.
Nitpick: this function name should have the word expect in it so that it makes it clear that when the function is called it's doing an assertion. Something like expectFilesToBeIdentical
packages/testlab/src/test-sandbox.ts
Outdated
| * Copies a file from src to the TestSandbox. | ||
| * Copies a file from src to the TestSandbox. If copying a `.js` file which | ||
| * has an accompanying `.js.map` file in the src file location, the dest file | ||
| * will have it's sourceMappingURL updated to point to the original file as |
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.
nitpick: it's -> its
94a1c51 to
63edd51
Compare
|
@bajtos Please review when possible |
bajtos
left a comment
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 is great, thank you @virkt25! I have one question to consider, see below.
|
|
||
| if (parse(src).ext === '.js' && pathExists(src + '.map')) { | ||
| const srcMap = src + '.map'; | ||
| await appendFile(dest, `\n//# sourceMappingURL=${srcMap}`); |
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.
IIUC, this assumes that the .js file does not contain any //# sourceMappingURL directive yet. Is it a safe assumption? Should we support the case when the .js file already contains sourceMappingURL directive?
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.
Nope. A file can contain the directive but source-map only considers the last directive for the sourceMappingURL.
The moment we compile a file it gets this directive ... this adds the directive again with the absolute path of the original map file as the last line (so the previous line is ignored as just a comment) after copying the file over to it's new destination.
When a `.ts` compled `.js` file is copied and required, a warning / error is logged to console because the accompanying `.js.map` file cannot be resolved. This PR updates the sourceMappingURL in the copied file to point to the original `.js.map` file as an absolute path so it can be resolved (only if accompanying `.js.map` file exists).
When a
.tscompled.jsfile is copied and required, a warning / error is logged to console because the accompanying.js.mapfile cannot be resolved. This PR updates the sourceMappingURL in the copied file to point to the original.js.mapfile as an absolute path so it can be resolved (only if accompanying.js.mapfile exists).Based on a review comment here: #858 (comment)
Checklist
npm testpasses on your machinepackages/cliwere updatedpackages/example-*were updated