Skip to content

Commit a070fce

Browse files
authored
Merge pull request #76 from bestander/e2e-tests-3
e2e tests for fbsource integration
2 parents 6d50320 + ec194bf commit a070fce

File tree

29 files changed

+413
-1
lines changed

29 files changed

+413
-1
lines changed

test/commands/install.js

Lines changed: 326 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { run as uninstall } from "../../src/cli/commands/uninstall.js";
1515
import Config from "../../src/config.js";
1616
import * as fs from "../../src/util/fs.js";
1717
import assert from "assert";
18+
import semver from "semver";
19+
1820

1921
let test = require("ava");
2022
let path = require("path");
@@ -335,6 +337,60 @@ test("upgrade scenario", () => {
335337
}, clean);
336338
});
337339

340+
test("upgrade scenario 2 (with sub dependencies)", async () => {
341+
// mime-types@2.0.0 is saved in local mirror and gets updated to mime-types@2.1.11
342+
// files in mirror, fbkpm.lock, package.json and node_modules should reflect that
343+
344+
let mirrorPath = "mirror-for-offline";
345+
let fixture = "install-upgrade-scenario-2";
346+
let cwd = path.join(fixturesLoc, fixture);
347+
await fs.copy(path.join(cwd, "fbkpm.lock.before"), path.join(cwd, "fbkpm.lock"));
348+
await fs.copy(path.join(cwd, "package.json.before"), path.join(cwd, "package.json"));
349+
350+
return run({}, [], fixture, async (config) => {
351+
assert(semver.satisfies(
352+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-db/package.json"))).version,
353+
"~1.0.1")
354+
);
355+
assert.equal(
356+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-types/package.json"))).version,
357+
"2.0.0"
358+
);
359+
360+
return run({save: true}, ["mime-types@2.1.11"], fixture, async (config) => {
361+
assert(semver.satisfies(
362+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-db/package.json"))).version,
363+
"~1.23.0"
364+
));
365+
assert.equal(
366+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-types/package.json"))).version,
367+
"2.1.11"
368+
);
369+
370+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
371+
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
372+
assert.equal(lockFileLines[0], "mime-db@~1.23.0:");
373+
assert.notEqual(lockFileLines[3].indexOf("resolved mime-db-"), -1);
374+
assert.equal(lockFileLines[4], "mime-types@2.1.11:");
375+
assert.notEqual(lockFileLines[7].indexOf("resolved mime-types-2.1.11.tgz"), -1);
376+
377+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
378+
assert.equal(mirror.length, 4);
379+
let newFilesInMirror = mirror.filter((elem) => {
380+
return elem.relative !== "mime-db-1.0.3.tgz" && elem.relative !== "mime-types-2.0.0.tgz";
381+
});
382+
383+
assert.equal(newFilesInMirror.length, 2);
384+
385+
await fs.unlink(newFilesInMirror[0].absolute);
386+
await fs.unlink(newFilesInMirror[1].absolute);
387+
388+
await fs.unlink(path.join(config.cwd, "fbkpm.lock"));
389+
await fs.unlink(path.join(config.cwd, "package.json"));
390+
});
391+
});
392+
});
393+
338394
test("downgrade scenario", () => {
339395
// left-pad first installed 1.1.0 then downgraded to 0.0.9
340396
// files in mirror, fbkpm.lock, package.json and node_modules should reflect that
@@ -535,7 +591,6 @@ test("uninstall should remove subdependencies", () => {
535591
await uninstall(config, reporter, {}, ["dep-a"]);
536592

537593
assert(!await fs.exists(path.join(config.cwd, "node_modules/dep-a")));
538-
// TODO dep-b did not get removed
539594
assert(!await fs.exists(path.join(config.cwd, "node_modules/dep-b")));
540595
assert(await fs.exists(path.join(config.cwd, "node_modules/dep-c")));
541596

@@ -562,4 +617,274 @@ test("uninstall should remove subdependencies", () => {
562617
});
563618
});
564619

620+
test("install --save should add missing deps to fbkpm and mirror (PR import scenario)", async () => {
621+
let mirrorPath = "mirror-for-offline";
622+
let fixture = "install-import-pr";
623+
let cwd = path.join(fixturesLoc, fixture);
624+
await fs.copy(path.join(cwd, "fbkpm.lock.before"), path.join(cwd, "fbkpm.lock"));
625+
626+
return run({save: true}, [], fixture, async (config, reporter) => {
627+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
628+
"node_modules/mime-types/package.json"))).version, "2.0.0");
629+
assert(semver.satisfies(JSON.parse(await fs.readFile(path.join(config.cwd,
630+
"node_modules/mime-db/package.json"))).version, "~1.0.1"));
631+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
632+
"node_modules/fake-fbkpm-dependency/package.json"))).version, "1.0.1");
633+
634+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
635+
assert.equal(mirror.length, 3);
636+
assert.equal(mirror[0].relative, "fake-fbkpm-dependency-1.0.1.tgz");
637+
assert.equal(mirror[1].relative.indexOf("mime-db-1.0."), 0);
638+
assert.equal(mirror[2].relative, "mime-types-2.0.0.tgz");
639+
640+
let lockFileContent = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
641+
let lockFileLines = lockFileContent.split("\n").filter((line) => !!line);
642+
// TODO error here
643+
assert.equal(lockFileLines.length, 14);
644+
assert.equal(lockFileLines[4].indexOf("mime-db@"), 0);
645+
assert.equal(lockFileLines[8].indexOf("mime-types@2.0.0"), 0);
646+
647+
await fs.unlink(path.join(mirror[1].absolute));
648+
await fs.unlink(path.join(mirror[2].absolute));
649+
await fs.unlink(path.join(config.cwd, "fbkpm.lock"));
650+
});
651+
});
652+
653+
654+
test("install --save should update a dependency to fbkpm and mirror (PR import scenario 2)", async () => {
655+
// mime-types@2.0.0 is saved in local mirror and gets updated to mime-types@2.1.11 via
656+
// a change in package.json,
657+
// files in mirror, fbkpm.lock, package.json and node_modules should reflect that
658+
659+
let mirrorPath = "mirror-for-offline";
660+
let fixture = "install-import-pr-2";
661+
let cwd = path.join(fixturesLoc, fixture);
662+
await fs.copy(path.join(cwd, "fbkpm.lock.before"), path.join(cwd, "fbkpm.lock"));
663+
await fs.copy(path.join(cwd, "package.json.before"), path.join(cwd, "package.json"));
664+
665+
return run({}, [], fixture, async (config) => {
666+
assert(semver.satisfies(
667+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-db/package.json"))).version,
668+
"~1.0.1")
669+
);
670+
assert.equal(
671+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-types/package.json"))).version,
672+
"2.0.0"
673+
);
674+
675+
await fs.unlink(path.join(config.cwd, "package.json"));
676+
await fs.copy(path.join(cwd, "package.json.after"), path.join(cwd, "package.json"));
677+
678+
return run({save: true}, [], fixture, async (config) => {
679+
assert(semver.satisfies(
680+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-db/package.json"))).version,
681+
"~1.23.0"
682+
));
683+
assert.equal(
684+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-types/package.json"))).version,
685+
"2.1.11"
686+
);
687+
688+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
689+
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
690+
assert.equal(lockFileLines[0], "mime-db@~1.23.0:");
691+
assert.notEqual(lockFileLines[3].indexOf("resolved mime-db-"), -1);
692+
assert.equal(lockFileLines[4], "mime-types@2.1.11:");
693+
assert.notEqual(lockFileLines[7].indexOf("resolved mime-types-2.1.11.tgz"), -1);
694+
695+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
696+
assert.equal(mirror.length, 4);
697+
let newFilesInMirror = mirror.filter((elem) => {
698+
return elem.relative !== "mime-db-1.0.3.tgz" && elem.relative !== "mime-types-2.0.0.tgz";
699+
});
700+
701+
assert.equal(newFilesInMirror.length, 2);
702+
703+
await fs.unlink(newFilesInMirror[0].absolute);
704+
await fs.unlink(newFilesInMirror[1].absolute);
705+
706+
await fs.unlink(path.join(config.cwd, "fbkpm.lock"));
707+
await fs.unlink(path.join(config.cwd, "package.json"));
708+
});
709+
});
710+
});
711+
712+
test("install --initMirror should add init mirror deps from package.json", async () => {
713+
let mirrorPath = "mirror-for-offline";
714+
let fixture = "install-init-mirror";
715+
716+
// initMirror gets converted to save flag in cli/install.js
717+
return run({save: true}, [], fixture, async (config, reporter) => {
718+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
719+
"node_modules/mime-types/package.json"))).version, "2.0.0");
720+
assert(semver.satisfies(JSON.parse(await fs.readFile(path.join(config.cwd,
721+
"node_modules/mime-db/package.json"))).version, "~1.0.1"));
722+
723+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
724+
assert.equal(mirror.length, 2);
725+
assert.equal(mirror[0].relative.indexOf("mime-db-1.0."), 0);
726+
assert.equal(mirror[1].relative, "mime-types-2.0.0.tgz");
727+
728+
let lockFileContent = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
729+
let lockFileLines = lockFileContent.split("\n").filter((line) => !!line);
730+
assert.equal(lockFileLines.length, 10);
731+
assert.equal(lockFileLines[0].indexOf("mime-db@"), 0);
732+
assert.equal(lockFileLines[4].indexOf("mime-types@2.0.0"), 0);
733+
734+
await fs.unlink(path.join(config.cwd, mirrorPath));
735+
await fs.unlink(path.join(config.cwd, "fbkpm.lock"));
736+
});
737+
});
738+
739+
test("install --save with new dependency should be deterministic", async () => {
740+
// mime-types@2.0.0->mime-db@1.0.3 is saved in local mirror and is deduped
741+
// install mime-db@1.23.0 should move mime-db@1.0.3 deep into mime-types
742+
743+
let mirrorPath = "mirror-for-offline";
744+
let fixture = "install-deterministic";
745+
let cwd = path.join(fixturesLoc, fixture);
746+
await fs.copy(path.join(cwd, "fbkpm.lock.before"), path.join(cwd, "fbkpm.lock"));
747+
await fs.copy(path.join(cwd, "package.json.before"), path.join(cwd, "package.json"));
748+
749+
return run({}, [], fixture, async (config) => {
750+
assert(semver.satisfies(
751+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-db/package.json"))).version,
752+
"~1.0.1")
753+
);
754+
assert.equal(
755+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-types/package.json"))).version,
756+
"2.0.0"
757+
);
758+
759+
return run({save: true}, ["mime-db@1.23.0"], fixture, async (config) => {
760+
assert(semver.satisfies(
761+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-db/package.json"))).version,
762+
"~1.23.0"
763+
));
764+
assert.equal(
765+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-types/package.json"))).version,
766+
"2.0.0"
767+
);
768+
assert.equal(
769+
JSON.parse(await fs.readFile(path.join(config.cwd,
770+
"node_modules/mime-types/node_modules/mime-db/package.json"))).version,
771+
"1.0.3"
772+
);
773+
assert.deepEqual(
774+
JSON.parse(await fs.readFile(path.join(config.cwd, "package.json"))).dependencies, {
775+
"mime-types": "2.0.0",
776+
"mime-db": "1.23.0"
777+
}
778+
);
779+
780+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
781+
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
782+
assert.equal(lockFileLines.length, 14);
783+
784+
785+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
786+
assert.equal(mirror.length, 3);
787+
assert.equal(mirror[1].relative, "mime-db-1.23.0.tgz");
788+
789+
await fs.unlink(mirror[1].absolute);
790+
await fs.unlink(path.join(config.cwd, "fbkpm.lock"));
791+
await fs.unlink(path.join(config.cwd, "package.json"));
792+
});
793+
});
794+
});
795+
796+
test("install --save with new dependency should be deterministic 2", async () => {
797+
// mime-types@2.0.0->mime-db@1.0.1 is saved in local mirror and is deduped
798+
// install mime-db@1.0.3 should replace mime-db@1.0.1 in root
799+
800+
let mirrorPath = "mirror-for-offline";
801+
let fixture = "install-deterministic-2";
802+
let cwd = path.join(fixturesLoc, fixture);
803+
await fs.copy(path.join(cwd, "fbkpm.lock.before"), path.join(cwd, "fbkpm.lock"));
804+
await fs.copy(path.join(cwd, "package.json.before"), path.join(cwd, "package.json"));
805+
806+
return run({}, [], fixture, async (config) => {
807+
assert.equal(
808+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-db/package.json"))).version,
809+
"1.0.1"
810+
);
811+
assert.equal(
812+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-types/package.json"))).version,
813+
"2.0.0"
814+
);
815+
816+
return run({save: true}, ["mime-db@1.0.3"], fixture, async (config) => {
817+
assert.equal(
818+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-db/package.json"))).version,
819+
"1.0.3"
820+
);
821+
assert.equal(
822+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/mime-types/package.json"))).version,
823+
"2.0.0"
824+
);
825+
assert(!await fs.exists(path.join(config.cwd, "node_modules/mime-types/node-modules/mime-db")));
826+
assert.deepEqual(
827+
JSON.parse(await fs.readFile(path.join(config.cwd, "package.json"))).dependencies, {
828+
"mime-types": "2.0.0",
829+
"mime-db": "1.0.3"
830+
}
831+
);
832+
833+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
834+
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
835+
assert.equal(lockFileLines.length, 10);
836+
837+
838+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
839+
assert.equal(mirror.length, 3);
840+
assert.equal(mirror[1].relative, "mime-db-1.0.3.tgz");
841+
842+
await fs.unlink(mirror[1].absolute);
843+
await fs.unlink(path.join(config.cwd, "fbkpm.lock"));
844+
await fs.unlink(path.join(config.cwd, "package.json"));
845+
});
846+
});
847+
});
848+
849+
850+
test("install --save should ignore cache", () => {
851+
// left-pad@1.1.0 gets installed without --save
852+
// left-pad@1.1.0 gets installed with --save
853+
// files in mirror, fbkpm.lock, package.json and node_modules should reflect that
854+
855+
let mirrorPath = "mirror-for-offline";
856+
857+
let fixture = "install-save-to-mirror-when-cached";
858+
return run({}, ["left-pad@1.1.0"], fixture, async (config) => {
859+
assert.equal(
860+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/left-pad/package.json"))).version,
861+
"1.1.0"
862+
);
863+
864+
return run({save: true}, ["left-pad@1.1.0"], fixture, async (config) => {
865+
assert.equal(
866+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/left-pad/package.json"))).version,
867+
"1.1.0"
868+
);
869+
assert.deepEqual(
870+
JSON.parse(await fs.readFile(path.join(config.cwd, "package.json"))).dependencies,
871+
{"left-pad": "1.1.0"}
872+
);
565873

874+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
875+
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
876+
assert.equal(lockFileLines[0], "left-pad@1.1.0:");
877+
assert.equal(lockFileLines.length, 4);
878+
assert.notEqual(lockFileLines[3].indexOf("resolved left-pad-1.1.0.tgz"), -1);
879+
880+
throw new Error("AA")
881+
882+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
883+
assert.equal(mirror.length, 1);
884+
assert.equal(mirror[0].relative, "left-pad-1.1.0.tgz");
885+
await fs.unlink(path.join(config.cwd, mirrorPath));
886+
await fs.unlink(path.join(config.cwd, "package.json"));
887+
888+
});
889+
});
890+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm-offline-mirror=./mirror-for-offline
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
mime-db@~1.0.1:
2+
name mime-db
3+
version "1.0.1"
4+
resolved mime-db-1.0.1.tgz#35d99b0965967253bb30633a7d07a8de9975a952
5+
mime-types@2.0.0:
6+
name mime-types
7+
version "2.0.0"
8+
resolved mime-types-2.0.0.tgz#4a85688446a4d94a03909e0ae292766744a3c313
9+
dependencies:
10+
mime-db "~1.0.1"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"mime-types": "2.0.0"
4+
}
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm-offline-mirror=./mirror-for-offline
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
mime-db@~1.0.1:
2+
name mime-db
3+
version "1.0.3"
4+
resolved mime-db-1.0.3.tgz#5680b12300b3cecea5620f20f269ee80f2632d81
5+
mime-types@2.0.0:
6+
name mime-types
7+
version "2.0.0"
8+
resolved mime-types-2.0.0.tgz#4a85688446a4d94a03909e0ae292766744a3c313
9+
dependencies:
10+
mime-db "~1.0.1"
Binary file not shown.

0 commit comments

Comments
 (0)