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

feat: update tests and correct type definition #46

Merged
merged 2 commits into from
Dec 4, 2019

Conversation

kyegupov
Copy link

@kyegupov kyegupov commented Nov 26, 2019

  • Ready for review
  • Follows CONTRIBUTING rules
  • Reviewed by Snyk internal team

What does this PR do?

Corrects two mistakes:

  • some tests were using .sort() on a PkgInfo[] list. Without the comparison function, this sort was doing nothing. The tests were passing just by accident.
  • PkgInfo.version was set to null sometimes and was advertised as null-able in the README. This was fixed, now only undefined should be used.

Since it introduces a light breakage (correction) of the library interface, this is being submitted as "feat" rather than "fix" or "tests".

@kyegupov kyegupov requested a review from a team as a code owner November 26, 2019 10:30
@kyegupov kyegupov self-assigned this Nov 26, 2019
@gjvis
Copy link
Contributor

gjvis commented Nov 26, 2019

@kyegupov why null? Is that valid/desirable? Where have you seen it used?

@kyegupov
Copy link
Author

@gjvis

pkgs[id] = info.version ? info : { ...info, version: null } as any;

{ name: 'foo', version: null },
]);
expect(depGraph.getDepPkgs().sort()).toEqual([
{ name: 'foo', version: null },

@darscan
Copy link
Contributor

darscan commented Nov 26, 2019

Just my 2 cents (disregard at will):

  • I'm not wild about the test helper abstraction. The fix could have been to simply add the already used depSort to the two lines that were missed. But either way, it's fine
  • This PR is really 2 commits: the test stuff, and the type change
  • The feature commit message could be better: think about how it will look in the release notes

@kyegupov
Copy link
Author

kyegupov commented Nov 26, 2019

@darscan the first thing I did was exactly adding missing "depSort". Turned out, it was way more than 2 lines: there also was a bunch of expect(pkgs.sort()).toEqual([..arrayLiteral..]) which also needed fixing. I've figured that having a clear helper method is a better strategy than hoping that the developer won't forget to add .sort(depSort) into every assertion.

Re two changes in one: I've discovered the "null" problem late, when writing the helper, and I'm not yet sure how to handle it: proceed with nulls or convert them to undefineds.

@darscan
Copy link
Contributor

darscan commented Nov 26, 2019

I've figured that having a clear helper method is a better strategy than hoping that the developer won't forget to add .sort(depSort) into every assertion

Cool, I'm sold

@darscan
Copy link
Contributor

darscan commented Nov 26, 2019

I'm not yet sure how to handle it: proceed with nulls or convert them to undefineds.

Yeh

> JSON.stringify({foo: null, bar: undefined})
'{"foo":null}'

@kyegupov
Copy link
Author

yup, either change is slightly breaking something: types or implementation

@darscan
Copy link
Contributor

darscan commented Nov 26, 2019

yup, either change is slightly breaking something: types or implementation

yeh. Out of curiosity.. if you don't change anything.. does anything actually break?

@kyegupov
Copy link
Author

@darscan the only things that could be broken are: pkg.version===null, pkg.hasOwnProperty('version'), JSON comparison, "deep equality" comparison in tests.

@@ -78,7 +78,7 @@ async function buildGraph(
eventLoopSpinner: EventLoopSpinner,
isRoot = false): Promise<string> {

const getNodeId = (name: string, version: string | undefined, hashId: string) =>
const getNodeId = (name: string, version: string|null|undefined, hashId: string) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

should better have spaces around the | - that's how it appears in the docs: https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types.

@michael-go
Copy link
Contributor

michael-go commented Nov 26, 2019

@kyegupov good catch that we actually set it to null in the code but the type is string | undefined.

  • note that this change breaks compilation in some of our current code-bases, but maybe it's ok? ...
  • maybe if we fix it, better fix it to be just string | null to make it simpler the code to set undefined instead of null if the version is nullish - and leave the type simple with .version?. Also @gjvis & @darscan think it feels more correct to have version "undefined" if it's unknown, rather than null ?

@kyegupov kyegupov force-pushed the feat/update-tests-and-types branch 4 times, most recently from c94ddaf to f56a991 Compare November 27, 2019 12:00
@@ -22,8 +22,7 @@ export function createFromJSON(depGraphData: DepGraphData): DepGraph {
const pkgNodes: {[pkgId: string]: Set<string>} = {};

for (const { id, info } of depGraphData.pkgs) {
// TODO: avoid this, instead just use `info` as is
pkgs[id] = info.version ? info : { ...info, version: null } as any;
pkgs[id] = info;
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe it's better to actually set it to undefined if it's nullish to make sure it adhers the type?

Copy link
Author

Choose a reason for hiding this comment

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

Yep, why not.

@@ -161,6 +161,10 @@ class DepGraphImpl implements types.DepGraphInternal {
otherDepGraph = createFromJSON(other.toJSON()) as types.DepGraphInternal;
}

// In theory, for the graphs created by standard means, `_.isEquals(this._data, otherDepGraph._data)`
// should suffice, since node IDs will be generated in a predictable way.
// However, to support unconventional node IDs, we run our own deep
Copy link
Contributor

Choose a reason for hiding this comment

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

not only - it's also to:

  • compare across outputs of different (compatible) versions of the lib
  • be not sensitive to ordering as the data contains arrays

Copy link
Author

Choose a reason for hiding this comment

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

the array ordering should be fairly deterministic, I believe (but that hinges on the current V8 implementation)

Copy link
Contributor

@michael-go michael-go left a comment

Choose a reason for hiding this comment

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

maybe we want to "coalesce" the null-ish versions we get when converting from a depTree to undefined? like e.g. in https://github.com/snyk/dep-graph/blob/master/src/legacy/index.ts#L48

@kyegupov
Copy link
Author

kyegupov commented Dec 4, 2019

@michael-go added coalecsion

@kyegupov kyegupov merged commit fc9ff9a into master Dec 4, 2019
@kyegupov kyegupov deleted the feat/update-tests-and-types branch December 4, 2019 12:52
@snyksec
Copy link

snyksec commented Dec 4, 2019

🎉 This PR is included in version 1.15.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

None yet

5 participants