Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Add CI and unit tests #29

Merged
merged 20 commits into from
Jul 8, 2020
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
node-version: [10.x, 12.x]
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
node_modules
package-lock.json
.DS_Store

dst.txt
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const fs = require("fs");
const text = fs.readFileSync(src_file);
mrcnski marked this conversation as resolved.
Show resolved Hide resolved
console.log(`Uploading file ${src_file} containing data: '${text}'`);

const skylink = await skynet.UploadFile(src_file, skynet.DefaultUploadOptions);
const skylink = await skynet.uploadFile(src_file, skynet.DefaultUploadOptions);
mrcnski marked this conversation as resolved.
Show resolved Hide resolved
if (!skylink.includes(skynet.UriSkynetPrefix)) {
console.log(`ERROR: invalid skylink returned: ${skylink}`);
return;
Expand All @@ -46,7 +46,7 @@ const fs = require("fs");

console.log(`Downloading to ${dst_file}`);
const new_skylink = skylink.replace(skynet.UriSkynetPrefix, "");
await skynet.DownloadFile(dst_file, new_skylink, skynet.DefaultDownloadOptions);
await skynet.downloadFile(dst_file, new_skylink, skynet.DefaultDownloadOptions);
mrcnski marked this conversation as resolved.
Show resolved Hide resolved
const data = fs.readFileSync(dst_file);
if (!data.equals(text)) {
console.log(`ERROR: wrong data returned: ${data}`);
Expand All @@ -59,7 +59,7 @@ const fs = require("fs");
const src_dir = "./src";

console.log(`Uploading dir ${src_dir}`);
const dir_skylink = await skynet.UploadDirectory(src_dir);
const dir_skylink = await skynet.uploadDirectory(src_dir);
if (!dir_skylink.includes(skynet.UriSkynetPrefix)) {
console.log(`ERROR: invalid skylink returned: ${dir_skylink}`);
return;
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use strict";

const { DefaultUploadOptions, UploadFile, UploadDirectory } = require("./src/upload");
const { DefaultDownloadOptions, DownloadFile } = require("./src/download");
const { DefaultUploadOptions, uploadFile, uploadDirectory } = require("./src/upload");
const { DefaultDownloadOptions, downloadFile } = require("./src/download");
const { UriSkynetPrefix } = require("./src/utils");

module.exports = {
DefaultDownloadOptions,
DefaultUploadOptions,
DownloadFile,
UploadDirectory,
UploadFile,
downloadFile,
uploadDirectory,
uploadFile,
UriSkynetPrefix,
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
],
"license": "MIT",
"dependencies": {
"axios": "^0.19.2",
"form-data": "^3.0.0"
"axios": "0.19.2",
"form-data": "3.0.0",
"tmp": "0.2.1"
mrcnski marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"eslint": "^7.2.0",
Expand Down
4 changes: 2 additions & 2 deletions src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const DefaultDownloadOptions = {
portalUrl: "https://siasky.net",
};

function DownloadFile(path, skylink, customOptions = {}) {
function downloadFile(path, skylink, customOptions = {}) {
const opts = { ...DefaultDownloadOptions, ...customOptions };

const url = `${trimTrailingSlash(opts.portalUrl)}/${trimSiaPrefix(skylink)}`;
Expand All @@ -30,4 +30,4 @@ function DownloadFile(path, skylink, customOptions = {}) {
});
}

module.exports = { DefaultDownloadOptions, DownloadFile };
module.exports = { DefaultDownloadOptions, downloadFile };
13 changes: 9 additions & 4 deletions src/download.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
const axios = require("axios");
const { DownloadFile } = require("./download");
const tmp = require("tmp");

const { downloadFile } = require("./download");

jest.mock("axios");

const portalUrl = "https://siasky.net";
const skylink = "XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg";
const tmpFile = tmp.fileSync();

describe("download", () => {
const dst_file = "./dst.txt";
const dst_file = tmpFile.name;
const body = "asdf";

axios.get.mockResolvedValue({ data: { body, pipe: function () {} } });
mrcnski marked this conversation as resolved.
Show resolved Hide resolved

it("should send get request to default portal", () => {
DownloadFile(dst_file, skylink);
downloadFile(dst_file, skylink);

expect(axios.get).toHaveBeenCalledWith(`${portalUrl}/${skylink}`, { responseType: "stream" });
});

it("should use custom options if defined", () => {
DownloadFile(dst_file, skylink, {
downloadFile(dst_file, skylink, {
portalUrl: "localhost",
});

expect(axios.get).toHaveBeenCalledWith(`localhost/${skylink}`, { responseType: "stream" });
});
});

tmpFile.removeCallback();
mrcnski marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 3 additions & 3 deletions src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DefaultUploadOptions = {
customFilename: "",
};

function UploadFile(path, customOptions = {}) {
function uploadFile(path, customOptions = {}) {
const opts = { ...DefaultUploadOptions, ...customOptions };

const formData = new FormData();
Expand All @@ -36,7 +36,7 @@ function UploadFile(path, customOptions = {}) {
});
}

function UploadDirectory(path, customOptions = {}) {
function uploadDirectory(path, customOptions = {}) {
const opts = { ...DefaultUploadOptions, ...customOptions };

// Check if there is a directory at given path.
Expand Down Expand Up @@ -67,4 +67,4 @@ function UploadDirectory(path, customOptions = {}) {
});
}

module.exports = { DefaultUploadOptions, UploadFile, UploadDirectory };
module.exports = { DefaultUploadOptions, uploadFile, uploadDirectory };
14 changes: 7 additions & 7 deletions src/upload.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const axios = require("axios");

const { UploadDirectory, UploadFile } = require("./upload");
const { uploadDirectory, uploadFile } = require("./upload");
const { UriSkynetPrefix } = require("./utils");

jest.mock("axios");
Expand All @@ -14,7 +14,7 @@ describe("uploadFile", () => {
axios.post.mockResolvedValue({ data: { skylink } });
mrcnski marked this conversation as resolved.
Show resolved Hide resolved

it("should send post request to default portal", () => {
UploadFile(filename);
uploadFile(filename);

expect(axios.post).toHaveBeenCalledWith(
`${portalUrl}/skynet/skyfile`,
Expand All @@ -28,7 +28,7 @@ describe("uploadFile", () => {
});

it("should use custom options if defined", () => {
UploadFile(filename, {
uploadFile(filename, {
portalUrl: "localhost",
portalUploadPath: "/skynet/file",
portalFileFieldname: "filetest",
Expand All @@ -47,7 +47,7 @@ describe("uploadFile", () => {
});

it("should return skylink on success", async () => {
const data = await UploadFile(filename);
const data = await uploadFile(filename);

expect(data).toEqual(UriSkynetPrefix + skylink);
});
Expand All @@ -60,7 +60,7 @@ describe("uploadDirectory", () => {
axios.post.mockResolvedValue({ data: { skylink } });
mrcnski marked this conversation as resolved.
Show resolved Hide resolved

it("should send post request to default portal", () => {
UploadDirectory(filename);
uploadDirectory(filename);

for (const file of directory) {
expect(axios.post).toHaveBeenCalledWith(
Expand All @@ -76,7 +76,7 @@ describe("uploadDirectory", () => {
});

it("should use custom options if defined", () => {
UploadDirectory(filename, {
uploadDirectory(filename, {
portalUrl: "localhost",
portalUploadPath: "/skynet/file",
portalDirectoryFileFieldname: "filetest",
Expand All @@ -97,7 +97,7 @@ describe("uploadDirectory", () => {
});

it("should return single skylink on success", async () => {
const data = await UploadDirectory(filename);
const data = await uploadDirectory(filename);

expect(data).toEqual(UriSkynetPrefix + skylink);
});
Expand Down
11 changes: 9 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.10.0.tgz#a17b3a8ea811060e74d47d306122400ad4497ae2"
integrity sha512-3YDiu347mtVtjpyV3u5kVqQLP242c06zwDOgpeRnybmXlYYsLbtTrUBUm8i8srONt+FWobl5aibnU1030PeeuA==

axios@^0.19.2:
[email protected]:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
Expand Down Expand Up @@ -1688,7 +1688,7 @@ forever-agent@~0.6.1:
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=

form-data@^3.0.0:
[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682"
integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==
Expand Down Expand Up @@ -3925,6 +3925,13 @@ through@^2.3.6, through@^2.3.8:
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=

tmp@*:
version "0.2.1"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.2.1.tgz#8457fc3037dcf4719c251367a1af6500ee1ccf14"
integrity sha512-76SUhtfqR2Ijn+xllcI5P1oyannHNHByD80W1q447gU3mp9G9PSpGdWmjUOHRDPiHYacIk66W7ubDTuPF3BEtQ==
dependencies:
rimraf "^3.0.0"

tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
Expand Down