Skip to content

Commit

Permalink
feat: add configuration tests (#411)
Browse files Browse the repository at this point in the history
chore: add configuration tests
  • Loading branch information
tenshiAMD committed Sep 13, 2022
1 parent e6c6571 commit 89dbc86
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 52 deletions.
57 changes: 26 additions & 31 deletions lib/modules/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ const { addContributorWithDetails } = require("all-contributors-cli");
const { AllContributorBotError } = require("./errors");

class Config {
constructor(repository) {
constructor(repository, options) {
this.repository = repository;

this.DEFAULT_OPTIONS = {
files: ["README.md"],
imageSize: 100,
commit: false,
commitConvention: "angular",
contributors: [],
contributorsPerLine: 7,
skipCi: true,
repoType: "github",
repoHost: "https://github.com"
};

this.options = Object.assign(this.DEFAULT_OPTIONS, options);
}

ensureValid() {
const { repo, owner } = this.repository;

this.get();

this.options.projectName = repo;
this.options.projectOwner = owner;
this.options.repoType = "github";
this.options.repoHost = "https://github.com";

if (typeof this.options.skipCi !== "boolean") {
this.options.skipCi = true;
}

if (!this.options.contributors) {
this.options.contributors = [];
}

if (!Array.isArray(this.options.contributors)) {
this.options.contributors = [];
}
}

async fetch() {
Expand Down Expand Up @@ -56,29 +58,25 @@ class Config {
}

init() {
this.options = {
files: ["README.md"],
imageSize: 100,
commit: false,
commitConvention: "angular",
contributors: [],
contributorsPerLine: 7,
};

this.get();
this.ensureValid();
}

get() {
const options = this.options;

if (!Array.isArray(options.files)) {
options.files = ["README.md"];
}

if (!Number.isInteger(options.contributorsPerLine)) {
options.contributorsPerLine = 7;
}
if (!("commitConvention" in options)) {
options.commitConvention = "angular";

if (!Array.isArray(options.contributors)) {
options.contributors = [];
}

return options;
}

Expand All @@ -98,6 +96,7 @@ class Config {
const options = this.options;

const contributors = options.contributors;
// TODO: Improve `contributors` mapping from CLI
for (let i = 0; i < contributors.length; i++) {
if (contributors[i].login === username) {
return contributors[i].contributions;
Expand All @@ -117,10 +116,6 @@ class Config {
async addContributor({ login, contributions, name, avatar_url, profile }) {
const options = this.options;

const profileWithProtocol = profile.startsWith("http")
? profile
: `http://${profile}`;

const oldContributions = this._findOldContributions(login);
const newContributions = [
...new Set([...oldContributions, ...contributions]),
Expand All @@ -132,7 +127,7 @@ class Config {
contributions: newContributions,
name,
avatar_url,
profile: profileWithProtocol,
profile: profile,
});
const newOptions = {
...options,
Expand Down
Loading

1 comment on commit 89dbc86

@vercel
Copy link

@vercel vercel bot commented on 89dbc86 Sep 13, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.