-
Notifications
You must be signed in to change notification settings - Fork 3
Default out path to {build}/{configuration}
directory and refactored pathSuffix
naming strategy
#150
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
Merged
Merged
Default out path to {build}/{configuration}
directory and refactored pathSuffix
naming strategy
#150
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b04c4f3
Default out path to {build}/{configuration} directory
kraenhansen d9cda60
Adjust weak-node-api output directory
kraenhansen 62900f2
Call findNodeAddonForBindings in plugin
kraenhansen b8b6b92
Refactor babel plugin tests
kraenhansen 883978b
Refactor naming strategy
kraenhansen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,208 @@ | ||
import assert from "node:assert/strict"; | ||
import { describe, it } from "node:test"; | ||
import { describe, it, TestContext } from "node:test"; | ||
import path from "node:path"; | ||
|
||
import { transformFileSync } from "@babel/core"; | ||
|
||
import { plugin } from "./plugin.js"; | ||
import { plugin, PluginOptions } from "./plugin.js"; | ||
import { setupTempDirectory } from "../test-utils.js"; | ||
import { getLibraryName } from "../path-utils.js"; | ||
|
||
describe("plugin", () => { | ||
it("transforms require calls, regardless", (context) => { | ||
const tempDirectoryPath = setupTempDirectory(context, { | ||
"package.json": `{ "name": "my-package" }`, | ||
"addon-1.apple.node/addon-1.node": | ||
"// This is supposed to be a binary file", | ||
"addon-2.apple.node/addon-2.node": | ||
"// This is supposed to be a binary file", | ||
"addon-1.js": ` | ||
const addon = require('./addon-1.node'); | ||
console.log(addon); | ||
`, | ||
"addon-2.js": ` | ||
const addon = require('./addon-2.node'); | ||
console.log(addon); | ||
`, | ||
"sub-directory/addon-1.js": ` | ||
const addon = require('../addon-1.node'); | ||
console.log(addon); | ||
`, | ||
"addon-1-bindings.js": ` | ||
const addon = require('bindings')('addon-1'); | ||
console.log(addon); | ||
`, | ||
"require-js-file.js": ` | ||
const addon = require('./addon-1.js'); | ||
console.log(addon); | ||
`, | ||
}); | ||
type TestTransformationOptions = { | ||
files: Record<string, string>; | ||
inputFilePath: string; | ||
assertion(code: string): void; | ||
options?: PluginOptions; | ||
}; | ||
|
||
const ADDON_1_REQUIRE_ARG = getLibraryName( | ||
path.join(tempDirectoryPath, "addon-1"), | ||
{ stripPathSuffix: false } | ||
); | ||
const ADDON_2_REQUIRE_ARG = getLibraryName( | ||
path.join(tempDirectoryPath, "addon-2"), | ||
{ stripPathSuffix: false } | ||
function itTransforms( | ||
title: string, | ||
{ files, inputFilePath, assertion, options = {} }: TestTransformationOptions | ||
) { | ||
it(`transforms ${title}`, (context: TestContext) => { | ||
const tempDirectoryPath = setupTempDirectory(context, files); | ||
const result = transformFileSync( | ||
path.join(tempDirectoryPath, inputFilePath), | ||
{ plugins: [[plugin, options]] } | ||
); | ||
assert(result, "Expected transformation to produce a result"); | ||
const { code } = result; | ||
assert(code, "Expected transformation to produce code"); | ||
assertion(code); | ||
}); | ||
} | ||
|
||
{ | ||
const result = transformFileSync( | ||
path.join(tempDirectoryPath, "./addon-1.js"), | ||
{ plugins: [[plugin, { stripPathSuffix: false }]] } | ||
); | ||
assert(result); | ||
const { code } = result; | ||
function assertIncludes(needle: string, { reverse = false } = {}) { | ||
return (code: string) => { | ||
if (reverse) { | ||
assert( | ||
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`), | ||
`Unexpected code: ${code}` | ||
!code.includes(needle), | ||
`Expected code to not include: ${needle}, got ${code}` | ||
); | ||
} | ||
|
||
{ | ||
const result = transformFileSync( | ||
path.join(tempDirectoryPath, "./addon-2.js"), | ||
{ plugins: [[plugin, { naming: "hash" }]] } | ||
); | ||
assert(result); | ||
const { code } = result; | ||
} else { | ||
assert( | ||
code && code.includes(`requireNodeAddon("${ADDON_2_REQUIRE_ARG}")`), | ||
`Unexpected code: ${code}` | ||
code.includes(needle), | ||
`Expected code to include: ${needle}, got ${code}` | ||
); | ||
} | ||
}; | ||
} | ||
|
||
{ | ||
const result = transformFileSync( | ||
path.join(tempDirectoryPath, "./sub-directory/addon-1.js"), | ||
{ plugins: [[plugin, { naming: "hash" }]] } | ||
); | ||
assert(result); | ||
const { code } = result; | ||
assert( | ||
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`), | ||
`Unexpected code: ${code}` | ||
); | ||
} | ||
describe("plugin", () => { | ||
describe("transforming require(...) calls", () => { | ||
itTransforms("a simple call", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"index.js": ` | ||
const addon = require('./my-addon.node'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
assertion: assertIncludes(`requireNodeAddon("my-package--my-addon")`), | ||
}); | ||
|
||
{ | ||
const result = transformFileSync( | ||
path.join(tempDirectoryPath, "./addon-1-bindings.js"), | ||
{ plugins: [[plugin, { naming: "hash" }]] } | ||
); | ||
assert(result); | ||
const { code } = result; | ||
assert( | ||
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`), | ||
`Unexpected code: ${code}` | ||
); | ||
} | ||
itTransforms("from sub-directory", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"sub-dir/index.js": ` | ||
const addon = require('../my-addon.node'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "sub-dir/index.js", | ||
assertion: assertIncludes(`requireNodeAddon("my-package--my-addon")`), | ||
}); | ||
|
||
{ | ||
const result = transformFileSync( | ||
path.join(tempDirectoryPath, "./require-js-file.js"), | ||
{ plugins: [[plugin, { naming: "hash" }]] } | ||
); | ||
assert(result); | ||
const { code } = result; | ||
assert( | ||
code && !code.includes(`requireNodeAddon`), | ||
`Unexpected code: ${code}` | ||
); | ||
} | ||
describe("in 'sub-dir'", () => { | ||
itTransforms("a nested addon (keeping suffix)", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"sub-dir/my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"index.js": ` | ||
const addon = require('./sub-dir/my-addon.node'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
options: { pathSuffix: "keep" }, | ||
assertion: assertIncludes( | ||
`requireNodeAddon("my-package--sub-dir-my-addon")` | ||
), | ||
}); | ||
|
||
itTransforms("a nested addon (stripping suffix)", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"sub-dir/my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"index.js": ` | ||
const addon = require('./sub-dir/my-addon.node'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
options: { pathSuffix: "strip" }, | ||
assertion: assertIncludes(`requireNodeAddon("my-package--my-addon")`), | ||
}); | ||
|
||
itTransforms("a nested addon (omitting suffix)", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"sub-dir/my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"index.js": ` | ||
const addon = require('./sub-dir/my-addon.node'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
options: { pathSuffix: "omit" }, | ||
assertion: assertIncludes(`requireNodeAddon("my-package")`), | ||
}); | ||
}); | ||
|
||
itTransforms("and does not touch required JS files", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
// TODO: Add a ./my-addon.node to make this test complete | ||
"my-addon.js": "// Some JS file", | ||
"index.js": ` | ||
const addon = require('./my-addon'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
assertion: assertIncludes("requireNodeAddon", { reverse: true }), | ||
}); | ||
}); | ||
|
||
describe("transforming require('binding')(...) calls", () => { | ||
itTransforms("a simple call", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"index.js": ` | ||
const addon = require('bindings')('my-addon'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
assertion: assertIncludes(`requireNodeAddon("my-package--my-addon")`), | ||
}); | ||
|
||
describe("in 'build/Release'", () => { | ||
itTransforms("a nested addon (keeping suffix)", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"build/Release/my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"index.js": ` | ||
const addon = require('bindings')('my-addon'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
options: { pathSuffix: "keep" }, | ||
assertion: assertIncludes( | ||
`requireNodeAddon("my-package--build-Release-my-addon")` | ||
), | ||
}); | ||
|
||
itTransforms("a nested addon (stripping suffix)", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"build/Release/my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"index.js": ` | ||
const addon = require('bindings')('my-addon'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
options: { pathSuffix: "strip" }, | ||
assertion: assertIncludes(`requireNodeAddon("my-package--my-addon")`), | ||
}); | ||
|
||
itTransforms("a nested addon (omitting suffix)", { | ||
files: { | ||
"package.json": `{ "name": "my-package" }`, | ||
"build/Release/my-addon.apple.node/my-addon.node": | ||
"// This is supposed to be a binary file", | ||
"index.js": ` | ||
const addon = require('bindings')('my-addon'); | ||
console.log(addon); | ||
`, | ||
}, | ||
inputFilePath: "index.js", | ||
options: { pathSuffix: "omit" }, | ||
assertion: assertIncludes(`requireNodeAddon("my-package")`), | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The
.default(false, "./{build}/{configuration}")
call will set the default tofalse
rather than the intended path string. Use.default("./{build}/{configuration}")
so--out
defaults correctly.Copilot uses AI. Check for mistakes.
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 on purpose, since we need the values of
build
andconfiguration
to construct the default value, we're setting it tofalse
to defer defining its actual value.