Skip to content

Conversation

@le-cong
Copy link
Contributor

@le-cong le-cong commented Apr 17, 2024

Fixes #75

@le-cong le-cong added the PATCH label Apr 17, 2024
@le-cong le-cong self-assigned this Apr 17, 2024
run: npm run ci:lint
- name: Run Tests
run: npm run ci:test
env:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

needed to pass tests which access npm

@le-cong le-cong requested review from adcreare and carlansley April 17, 2024 19:37
@le-cong
Copy link
Contributor Author

le-cong commented Apr 17, 2024

introducing a new step in publish-beta.yml (between Publish Beta and Comment npm publish result), e.g. smartdata test

async function obtainNpmPackage(packageVersion: string, workFolder: string): Promise<void> {
await addNPMRCFile(workFolder);

const fullCommandLine = `npm pack ${packageVersion}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

could we do an npm install somewhere instead? then we could do an import() and make sure all the machinery works (including the package.json is ok)

package.json Outdated
{
"name": "@checkdigit/github-actions",
"version": "2.0.0",
"version": "2.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

since we're adding a new action, think this should be a MINOR

async function verifyImportEntryPoints(packageFolder: string): Promise<void> {
const packageJson = JSON.parse(await fs.readFile(`${packageFolder}/package.json`, 'utf8')) as PackageJson;
const isEsm = packageJson.type === 'module';
const bundleFolder = isEsm ? 'dist-mjs' : 'dist';
Copy link
Contributor

Choose a reason for hiding this comment

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

if npm install isn't an option, think it would be better to simulate what node does, i.e. read the package.json and figure out the entry point from that.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think npm install and then doing what deployer does (import and then ping operation) would be good.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is for any package (not necessarily a service)? I was thinking an npm install + importing it from some generated typescript. then compile & run. that way you know the types are ok and the entry point is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for services, the "import+compile" way might not be able to verify their api endpoints which are not exported. many of their root index.ts only export swagger.ts, like:

// index.ts

export * as v1 from './api/v1/swagger';

so even if there is an issue, it might not be exposed.

Copy link
Contributor

Choose a reason for hiding this comment

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

in the new-new world a service is started by awaiting the default export of the main entry point. so this would expose an issue with API/service code once that starts rolling out. also, I feel the primary job of this action is to validate the npm package itself, which is effectively whatever is exported from the root index.ts, vs functionality as a service. we have other things that do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that makes total sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is probably a known limitation using import: if the package references excluded(e.g. from *.test.ts) types or values but doesn't export them through its root index.ts, then no error will be reported.
on the other hand, using node cli to load that package's index.ts can expose the problem though.

@le-cong
Copy link
Contributor Author

le-cong commented Apr 18, 2024

as part of the latest commit, i've updated tests quite a bit mainly because:

  • the check-label tests are tangled with the current version, which makes the test tricky and flaky;
  • fixed other flaky tests dealing with temporary folders
  • refactored github nock to make the usage of different PRs easier to understand

@le-cong le-cong requested a review from carlansley April 18, 2024 23:56
Copy link
Contributor

@adcreare adcreare left a comment

Choose a reason for hiding this comment

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

LGTM - very comprehensive

Copy link
Contributor

Choose a reason for hiding this comment

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

this can be updated to use 20.x/22.x now


import main from './validate-npm-package';

main(getInput('betaPackage'))
Copy link
Contributor

Choose a reason for hiding this comment

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

i forget what issues there might be with this with actions, but can we not use top-level await here? its being compiled to an .mjs file, figure it should Just Work?

@le-cong le-cong requested a review from carlansley April 29, 2024 20:17
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});
await main();
Copy link
Contributor

Choose a reason for hiding this comment

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

given we're just awaiting main() at the top level of the module, do we need the main function at all, just make everything top level? this feels very script-y anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good idea

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tried that and making everything top level, it works but there is one advantage in having a 'main' function when it comes to testing.
what happens is that we'll have to do dynamic import in the tests; and before the dynamic import we normally setup nocking and environment variables. Jest has its own module catching mechanism, which causes some of the tests start failing. one trick is to do jest.resetModules() everywhere.
it seems easier and less surprise if we call the main function instead.
what do you think? should we make the change, or keep the main?

Copy link
Contributor

Choose a reason for hiding this comment

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

if we're await()ing main, isn't it effectively having everything top-level anyway? my thought is we move main() into a separate module, and all the index.ts does is import and await it. that way tests can do their thing without side effects going on at the module level.

@le-cong le-cong requested a review from carlansley April 30, 2024 21:13
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});
await main();
Copy link
Contributor

Choose a reason for hiding this comment

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

if we're await()ing main, isn't it effectively having everything top-level anyway? my thought is we move main() into a separate module, and all the index.ts does is import and await it. that way tests can do their thing without side effects going on at the module level.

@github-actions
Copy link

github-actions bot commented Jun 4, 2024

Coverage after merging validate-npm-package into main will be

58.72%▴ +4.17%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
src/check-imports
   check-imports.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–38, 4–9
   index.ts0%0%0%0%1, 1–5
   package-lock-file-util.ts100%100%100%100%
   packages-not-allowed.ts100%100%100%100%
src/check-label
   check-label.ts94.23%89.47%100%95.12%62–64, 71–73
   index.ts0%0%0%0%1, 1–5
src/check-pr-reviews
   check-pr-reviews.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50, 6–9
   index.ts0%0%0%0%1, 1–5
src/check-published
   check-published.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50–59, 6, 60–65, 7–9
   index.ts0%0%0%0%1, 1–5
   slack.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50–59, 6–9
src/comment-npm-publish
   comment-npm-publish.ts0%0%0%0%1, 1, 10–19, 2, 20–22, 3–9
   index.ts0%0%0%0%1, 1–5
src/coverage-reporter
   cli.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50, 6–9
   comment.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50–52, 6–9
   coverage-reporter.ts0%0%0%0%1, 1, 10, 100–109, 11–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50–59, 6, 60–69, 7, 70–79, 8, 80–89, 9, 90–99
   delete-old-comments.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–49, 5, 50–59, 6, 60–69, 7, 70–72, 8–9
   get-changes.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–41, 5–9
   html.ts100%100%100%100%
   index.ts0%0%0%0%1, 1–5
   lcov.ts93.97%90%100%94.32%122–132, 139
   options.ts0%0%0%0%1, 1, 10–14, 2–9
   tabulate.ts99.56%97.96%100%100%159
   util.ts100%100%100%100%
src/github-api
   index.ts80.42%68.42%100%82.42%109–112, 134–137, 156–165, 180–183, 187–190, 203–206, 210–213, 224–227, 238–240, 246–248, 250–253, 49, 55, 57–61, 70–73, 77–80, 95–97
src/nocks
   github.test.ts98.95%90.91%100%99.44%164, 64
src/perform-bundle
   analyze.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–36, 4–9
   deployer.ts0%0%0%0%1, 1, 10–18, 2–9
   index.ts0%0%0%0%1, 1–5
   perform-bundle.ts0%0%0%0%1, 1, 10–19, 2, 20–29, 3, 30–39, 4, 40–42, 5–9
src/prepare-beta
   index.ts0%0%0%0%1, 1–5
   package.ts93.33%66.67%100%95%22–24
   prepare-beta.ts0%0%0%0%1, 1, 10–17, 2–9
src/publish-beta
   compile.ts0%0%0%0%1, 1, 10–14, 2–9
   files.ts92.86%91.67%100%92.86%28–31
   index.ts0%0%0%0%1, 1–5
   package.ts94.92%83.33%100%96%22–24
   publish-beta.ts0%0%0%0%1, 1, 10–19, 2, 20–28, 3–9
   publish.ts82.35%100%50%82.76%25–29
src/validate-npm-package
   index.ts0%0%0%0%1, 1–5
   validate-npm-package.ts100%100%100%100%

@le-cong le-cong requested a review from carlansley June 4, 2024 22:15
Copy link
Contributor

@carlansley carlansley left a comment

Choose a reason for hiding this comment

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

lftm, the only issue i can think of is would this work with packages that export weird things (e.g. *-config repos). e.g. jest-config doesn't export anything at all (maybe it should?). i suppose we can cross that bridge when we come to it.

@github-actions
Copy link

github-actions bot commented Jun 5, 2024

✅ PR review status - All reviews completed and approved!

@le-cong le-cong merged commit 6c6faab into main Jun 5, 2024
@le-cong le-cong deleted the validate-npm-package branch June 5, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validate published npms

4 participants