You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.8 KiB

6 years ago
  1. const path = require("path");
  2. const fs = require("fs");
  3. const cmd=require("node-cmd");
  4. const util = require("util");
  5. const assert = require("assert");
  6. const claimUtils = require("../src/claimUtils.js");
  7. cmd.get[util.promisify.custom] = (c) => {
  8. return new Promise((resolve, reject) => {
  9. cmd.get(c, (err, data, stderr) => {
  10. if (err) {
  11. reject(err);
  12. } else {
  13. resolve([data, stderr]);
  14. }
  15. });
  16. });
  17. };
  18. const getAsync = util.promisify(cmd.get);
  19. const mkdir = util.promisify(fs.mkdir);
  20. const writeFile = util.promisify(fs.writeFile);
  21. describe("command line", () => {
  22. let tmpPath;
  23. before(async () => {
  24. tmpPath = path.join(__dirname, "..", "tmp");
  25. if (!fs.existsSync(tmpPath)) {
  26. await mkdir(tmpPath, 0o744);
  27. }
  28. process.chdir(tmpPath);
  29. });
  30. it("Should create a tree from a claim files", async () => {
  31. let i;
  32. let claims = [];
  33. for (i=0; i<100; i++) {
  34. const b = Buffer.from([ i / 256, i % 256 ]);
  35. claims[i] = claimUtils.buildClaim("0x01", "0x02", "0x03", b).toString("hex");
  36. }
  37. claims = claims.sort();
  38. const claimsFile = path.join(tmpPath, "claims100.hex");
  39. const dbFile = path.join(tmpPath, "claims100.db");
  40. await writeFile(claimsFile, claims.join("\n"), "utf8");
  41. await getAsync(`${path.join("..", "cli.js")} -d ${dbFile} add ${claimsFile} `);
  42. const data = await getAsync(`${path.join("..", "cli.js")} -d ${dbFile} export`);
  43. let claims2 = data[0].split("\n");
  44. claims2 = claims2.filter(function(n){ return n.length>0; });
  45. claims2 = claims2.sort();
  46. assert.equal(claims2.join("\n"), claims.join("\n"));
  47. }).timeout(20000);
  48. });