Skip to content

Commit

Permalink
codebase: refactor and improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghustavh97 committed Oct 16, 2020
1 parent 0e76538 commit c968c7e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 65 deletions.
49 changes: 17 additions & 32 deletions src/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,33 @@ const Location = function (instance, source, destination) {
throw error.invalidPathError("destination", destination);
}
}
this.original = {
source: source,
};
this.original = { source };
var locations = { source, destination };
for (var location in locations) {
locations[location] = this.makeAbsolute(locations[location]);
locations[location] = locations[location].replace(/\\/gu, "/");
locations[location] = path.resolve(locations[location]).replace(/\\/gu, "/");
}
source = locations.source;
source = this.processSource(source);
destination = locations.destination;
this.source = source;
this.destination = destination;
this.source = this.process(locations.source);
this.destination = locations.destination;
};

Location.prototype = {
basename: function (location) {
if (process.platform == "win32") {
return path.win32.basename(location);
}

return path.posix.basename(location);
},
exists: function (location) {
return fs.existsSync(location);
},
makeAbsolute: function (location) {
if (!path.isAbsolute(location)) {
location = path.resolve(location);
}

return location;
},
toGlob: function (location) {
return fg.sync(path.join(location, path.join("/", "*.svg")).replace(/\\/gu, "/"));
},
processSource: function (source) {
if (is.pathToDir(source)) {
source = this.toGlob(source);
} else {
source = [source];
process: function (source) {
source = [source];
if (is.pathToDir(source[0])) {
source = fg.sync(
path.join(source[0], path.join("/", "*.svg")).replace(/\\/gu, "/")
);
}
source.forEach((svgPath) => {
if (!is.pathToFile(svgPath) || path.extname(svgPath) != ".svg") {
throw new Error(
`one of the source file paths does not point to a .svg file. ${svgPath}`
);
}
});

return source;
},
Expand Down
2 changes: 1 addition & 1 deletion src/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Option.prototype = {
},
update: function (options) {
for (var key in options) {
if (Object.prototype.hasOwnProperty.call(options, key)) {
if (Object.prototype.hasOwnProperty.call(this.data, key)) {
this.data[key] = options[key];
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const fs = require("fs");
const path = require("path");
const Svg = require("./svg");
const colors = require("colors");
const is = require("oslllo-validator");
const cliprogress = require("cli-progress");

const Processor = function (fixer) {
Expand All @@ -25,13 +24,8 @@ Processor.prototype = {
svgs = svgs.map((source) => {
var destination = path.join(
this.destination,
this.fixer.location.basename(source)
path.basename(source)
);
if (!is.pathToFile(source) || path.extname(source) != ".svg") {
throw new Error(
`expected a direct path to a svg file, ${source} was given.`
);
}

return { source, destination };
});
Expand Down
16 changes: 8 additions & 8 deletions src/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const Svg = function (path) {
this.svg2 = Svg2(this.path);
this.element = this.svg2.toElement();
this.outerHTML = this.element.outerHTML;
this.resized = this._resized();
this.original = this._original();
this.scale = this._scale();
this.resized = this.getResized();
this.original = this.getOriginal();
this.scale = this.getScale();
};

Svg.prototype = {
_resized: function () {
getResized: function () {
var element = Svg2(this.outerHTML).svg
.resize({
width: 600,
Expand All @@ -28,7 +28,7 @@ Svg.prototype = {

return { element, svg2, dimensions };
},
_original: function () {
getOriginal: function () {
var element = this.element.cloneNode(true);
var dimensions = this.svg2.svg.dimensions();
var attributes = Object.values(element.attributes).map(function (attribute) {
Expand All @@ -37,10 +37,10 @@ Svg.prototype = {

return { element, dimensions, attributes };
},
_scale: function () {
getScale: function () {
return this.original.dimensions.height / this.resized.dimensions.height;
},
_restore: function (outerHTML) {
toOriginal: function (outerHTML) {
var element = Svg2(outerHTML).toElement();
while (element.attributes.length > 0) {
element.removeAttribute(element.attributes[0].name);
Expand All @@ -59,7 +59,7 @@ Svg.prototype = {
process: async function () {
var pngBuffer = await this.resized.svg2.png().toBuffer();
var traced = await Potrace(pngBuffer, { svgSize: this.scale }).trace();
traced = this._restore(traced);
traced = this.toOriginal(traced);

return traced;
},
Expand Down
1 change: 1 addition & 0 deletions test/assets/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
failed-icons/
fixed-icons/
temp-fixed-icons/
Binary file added test/assets/images/svg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions test/src/test.exceptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const path = require("path");
const fs = require("fs-extra");
const is = require("oslllo-validator");
const error = require("../../src/error");
const { SVGFixer, assert, path2 } = require("./helper");
Expand Down Expand Up @@ -33,6 +34,16 @@ describe("test.exceptions", () => {
);
});
});
it("throws if source file is not a svg file", () => {
var source = path.resolve("test/assets/images/svg.png");
var destination = path2.fixed.relative;
assert.isTrue(fs.existsSync(source), "source test image does not exist");
assert.throws(
() => SVGFixer(source, destination),
Error,
`one of the source file paths does not point to a .svg file. ${source}`
);
});
});

describe("invalid destination arguments", () => {
Expand Down Expand Up @@ -71,6 +82,12 @@ describe("test.exceptions", () => {
);
});
});
it("does not throw if destination folder does not exist and options.throwIfDestinationDoesNotExist is set to false", () => {
var source = path2.single.absolute;
var destination = "test/assets/temp-fixed-icons";
var options = { throwIfDestinationDoesNotExist: false };
assert.doesNotThrow(() => SVGFixer(source, destination, options));
});
});

describe("options", () => {
Expand Down
55 changes: 38 additions & 17 deletions test/src/test.parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,43 @@
const { SVGFixer, assert, path2 } = require("./helper");

describe("test.parameters", () => {
it("can set the options parameter", async () => {
var options = {
showProgressBar: true,
throwIfDestinationDoesNotExist: false,
};
var instance = await SVGFixer(
path2.direct.absolute,
path2.fixed.absolute,
options
).fix();
var set = instance.options.all();
var keys = {
set: Object.values(set),
options: Object.values(options),
};
assert.deepEqual(keys.set, keys.options, "option parameter keys do not match");
assert.deepEqual(set, options, "option parameters do not match");
describe("options parameters", () => {
it("can set the options parameter", async () => {
var options = {
showProgressBar: true,
throwIfDestinationDoesNotExist: false,
};
var instance = await SVGFixer(
path2.direct.absolute,
path2.fixed.absolute,
options
).fix();
var set = instance.options.all();
var keys = {
set: Object.values(set),
options: Object.values(options),
};
assert.deepEqual(
keys.set,
keys.options,
"option parameter keys do not match"
);
assert.deepEqual(set, options, "option parameters do not match");
});
it("won't update invalid options parameters", () => {
var valid = {
showProgressBar: false,
throwIfDestinationDoesNotExist: true,
};
var invalid = {
invalidoptions: true,
};
var instance = SVGFixer(path2.direct.absolute, path2.fixed.absolute, invalid);
assert.deepEqual(
valid,
instance.options.all(),
"option parameters do not match"
);
});
});
});

0 comments on commit c968c7e

Please sign in to comment.