From cc9bd5b51c9c5a08f1f3d6166684d27fef8f61ca Mon Sep 17 00:00:00 2001 From: Michael Currin Date: Sun, 14 Jun 2020 10:49:51 +0200 Subject: [PATCH 1/6] chore: Create package --- .gitignore | 1 + package.json | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 .gitignore create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..c2658d7d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/package.json b/package.json new file mode 100644 index 00000000..fff190a5 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "badge-generator", + "description": "Online tool to help you quickly generate tailor-made badges/shields for your repo docs and learn to work with badges", + "main": "main.js", + "scripts": { + "test": "jest" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/MichaelCurrin/badge-generator.git" + }, + "author": "Michael Currin", + "license": "MIT", + "bugs": { + "url": "https://github.com/MichaelCurrin/badge-generator/issues" + }, + "homepage": "https://github.com/MichaelCurrin/badge-generator#readme", + "devDependencies": { + "jest": "^26.0.1" + } +} \ No newline at end of file From 1ae17df36782d9a5d9700ac8b84ba715378448fa Mon Sep 17 00:00:00 2001 From: Michael Currin Date: Sun, 14 Jun 2020 10:50:04 +0200 Subject: [PATCH 2/6] test: Create repo and license tests --- src/license.test.js | 34 ++++++++++++++++++++++++++++++++++ src/repo.test.js | 13 +++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/license.test.js create mode 100644 src/repo.test.js diff --git a/src/license.test.js b/src/license.test.js new file mode 100644 index 00000000..4651feee --- /dev/null +++ b/src/license.test.js @@ -0,0 +1,34 @@ +const { License } = require('./license.js') + +test('MIT license badge title displays correctly', () => { + var license = new License('MIT') + + expect(license.title()).toBe('License: MIT') +}) + +test('MIT license badge image is correct', () => { + var license = new License('MIT') + + expect(license.img()).toBe('https://img.shields.io/badge/License-MIT-blue') +}) + +test('MIT license badge relative target is correct', () => { + var license = new License('MIT') + + expect(license.REL_TARGET).toBe('#license') +}) + +test('MIT license full badge with relative target displays correctly', () => { + var license = new License('MIT') + + var badge = '[![License: MIT](https://img.shields.io/badge/License-MIT-blue)](#license)'; + expect(license.markdown()).toBe(badge) +}) + +test('MIT license full badge with absolute target displays correctly', () => { + var target = 'https://github.com/my-user/my-repo-name/blob/master/LICENSE'; + var license = new License('MIT', target) + + var badge = '[![License: MIT](https://img.shields.io/badge/License-MIT-blue)](https://github.com/my-user/my-repo-name/blob/master/LICENSE)'; + expect(license.markdown()).toBe(badge) +}) diff --git a/src/repo.test.js b/src/repo.test.js new file mode 100644 index 00000000..b6c7995a --- /dev/null +++ b/src/repo.test.js @@ -0,0 +1,13 @@ +const { Repo } = require('./repo.js') + +test('Get correct full URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.url()).toBe('https://github.com/my-user/my-repo-name') +}) + +test('Get correct full license URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.licenseUrl()).toBe('https://github.com/my-user/my-repo-name/blob/master/LICENSE') +}) From 2470c349cbd5d7a7fdda005f4b3b6827a6a93d35 Mon Sep 17 00:00:00 2001 From: Michael Currin Date: Sun, 14 Jun 2020 10:50:14 +0200 Subject: [PATCH 3/6] feat: And license.js and repo.js --- src/license.js | 32 ++++++++++++++++++++++++++++++++ src/repo.js | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/license.js create mode 100644 src/repo.js diff --git a/src/license.js b/src/license.js new file mode 100644 index 00000000..443a9b4a --- /dev/null +++ b/src/license.js @@ -0,0 +1,32 @@ +class License { + DOMAIN = 'https://img.shields.io' + REL_TARGET = '#license' + DEFAULT_COLOR = 'blue' + + constructor(type, target = null, color = null) { + this.type = type + + this.target = target || this.REL_TARGET + this.color = color || this.DEFAULT_COLOR + } + + title() { + return `License: ${this.type}` + } + + img() { + return `${this.DOMAIN}/badge/License-${this.type}-${this.color}` + } + + fileUrl() { + return `` + } + + markdown() { + return `[![${this.title()}](${this.img()})](${this.target})` + } +} + +module.exports = { + License, +} \ No newline at end of file diff --git a/src/repo.js b/src/repo.js new file mode 100644 index 00000000..d9c02232 --- /dev/null +++ b/src/repo.js @@ -0,0 +1,19 @@ +class Repo { + constructor(username, repoName) { + this.username = username + this.repoName = repoName + } + + url() { + return `https://github.com/${this.username}/${this.repoName}` + } + + licenseUrl() { + return `${this.url()}/blob/master/LICENSE` + } +} + + +module.exports = { + Repo +} \ No newline at end of file From 919666a8893f6c39caf3e2032848de1a3543c2ef Mon Sep 17 00:00:00 2001 From: Michael Currin Date: Sun, 14 Jun 2020 10:58:08 +0200 Subject: [PATCH 4/6] test: Move files --- src/{ => __tests__}/license.test.js | 2 +- src/{ => __tests__}/repo.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{ => __tests__}/license.test.js (96%) rename src/{ => __tests__}/repo.test.js (90%) diff --git a/src/license.test.js b/src/__tests__/license.test.js similarity index 96% rename from src/license.test.js rename to src/__tests__/license.test.js index 4651feee..dcfef753 100644 --- a/src/license.test.js +++ b/src/__tests__/license.test.js @@ -1,4 +1,4 @@ -const { License } = require('./license.js') +const { License } = require('../license.js') test('MIT license badge title displays correctly', () => { var license = new License('MIT') diff --git a/src/repo.test.js b/src/__tests__/repo.test.js similarity index 90% rename from src/repo.test.js rename to src/__tests__/repo.test.js index b6c7995a..deec35f0 100644 --- a/src/repo.test.js +++ b/src/__tests__/repo.test.js @@ -1,4 +1,4 @@ -const { Repo } = require('./repo.js') +const { Repo } = require('../repo.js') test('Get correct full URL for a repo', () => { var repo = new Repo('my-user', 'my-repo-name') From ccb5200ad6aaa0db953478313383156d5727af9c Mon Sep 17 00:00:00 2001 From: Michael Currin Date: Sun, 14 Jun 2020 11:06:33 +0200 Subject: [PATCH 5/6] chore: Move to components --- src/__tests__/license.test.js | 2 +- src/__tests__/repo.test.js | 2 +- src/{ => components}/license.js | 0 src/{ => components}/repo.js | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename src/{ => components}/license.js (100%) rename src/{ => components}/repo.js (100%) diff --git a/src/__tests__/license.test.js b/src/__tests__/license.test.js index dcfef753..326e02bc 100644 --- a/src/__tests__/license.test.js +++ b/src/__tests__/license.test.js @@ -1,4 +1,4 @@ -const { License } = require('../license.js') +const { License } = require('../components/license.js') test('MIT license badge title displays correctly', () => { var license = new License('MIT') diff --git a/src/__tests__/repo.test.js b/src/__tests__/repo.test.js index deec35f0..01703871 100644 --- a/src/__tests__/repo.test.js +++ b/src/__tests__/repo.test.js @@ -1,4 +1,4 @@ -const { Repo } = require('../repo.js') +const { Repo } = require('../components/repo.js') test('Get correct full URL for a repo', () => { var repo = new Repo('my-user', 'my-repo-name') diff --git a/src/license.js b/src/components/license.js similarity index 100% rename from src/license.js rename to src/components/license.js diff --git a/src/repo.js b/src/components/repo.js similarity index 100% rename from src/repo.js rename to src/components/repo.js From ce738b0e0f8ad281495b26d81e6c464e749c8a3e Mon Sep 17 00:00:00 2001 From: Michael Currin Date: Sun, 14 Jun 2020 12:22:00 +0200 Subject: [PATCH 6/6] test: Create and update tests --- src/__tests__/external-repo-badge.test.js | 0 src/__tests__/license.test.js | 2 +- src/__tests__/made-with-badge.test.js | 6 ++ src/__tests__/markdown-link.test.js | 36 ++++++++++++ src/__tests__/repo.test.js | 71 ++++++++++++++++++++++- 5 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 src/__tests__/external-repo-badge.test.js create mode 100644 src/__tests__/made-with-badge.test.js create mode 100644 src/__tests__/markdown-link.test.js diff --git a/src/__tests__/external-repo-badge.test.js b/src/__tests__/external-repo-badge.test.js new file mode 100644 index 00000000..e69de29b diff --git a/src/__tests__/license.test.js b/src/__tests__/license.test.js index 326e02bc..f68fd9dc 100644 --- a/src/__tests__/license.test.js +++ b/src/__tests__/license.test.js @@ -25,7 +25,7 @@ test('MIT license full badge with relative target displays correctly', () => { expect(license.markdown()).toBe(badge) }) -test('MIT license full badge with absolute target displays correctly', () => { +test('MIT license full badge with FQDN target displays correctly', () => { var target = 'https://github.com/my-user/my-repo-name/blob/master/LICENSE'; var license = new License('MIT', target) diff --git a/src/__tests__/made-with-badge.test.js b/src/__tests__/made-with-badge.test.js new file mode 100644 index 00000000..c35cf741 --- /dev/null +++ b/src/__tests__/made-with-badge.test.js @@ -0,0 +1,6 @@ +test('Made with Bash badge is correct', () => { + var badge = new MadeWith('Bash') + + // Use mix of shield and markdown links internally. And preset target. + var result = '[![Made with Bash](https://img.shields.io/badge/Made_with-Bash-blue.svg)](https://www.gnu.org/software/bash/)' +}) diff --git a/src/__tests__/markdown-link.test.js b/src/__tests__/markdown-link.test.js new file mode 100644 index 00000000..d61a5e50 --- /dev/null +++ b/src/__tests__/markdown-link.test.js @@ -0,0 +1,36 @@ +const { MarkdownLink } = require('../components/markdownLink.js') + +test('An ID markdown link formats correctly', () => { + var tag = MarkdownLink('My link', '#target') + expect(tag.md()).toBe('[My link](#target)') +}) + +test('An absolute markdown link formats correctly', () => { + var tag = MarkdownLink('Absolute link', '/foo/bar.md') + expect(tag.md()).toBe('[Absolute link](/foo/bar.md)') +}) + +test('A markdown URL link formats correctly', () => { + var tag = MarkdownLink('Example link', 'https://example.com') + expect(tag.md()).toBe('[Example link](https://example.com)') +}) + +test('A markdown image tag formats correctly', () => { + var tag = MarkdownLink('Example image', 'https://example.com/image.png') + expect(tag.image()).toBe('![Example image](https://example.com/image.png)') +}) + +test('A markdown image tag with target URL formats correctly', () => { + var tag = MarkdownLink('Example image', 'https://example.com/image.png', 'https://foo.bar.com') + expect(tag.image()).toBe('[![Example image](https://example.com/image.png)](https://foo.bar.com)') +}) + +test('An ID markdown link with alt text formats correctly', () => { + var tag = MarkdownLink('My link', '#target') + expect(tag.md("My alt test for hover")).toBe('[My link](#target "My alt test for hover")') +}) + +test('A markdown image tag with alt text and target URL formats correctly', () => { + var tag = MarkdownLink('Example image', 'https://example.com/image.png', 'https://foo.bar.com') + expect(tag.image("My alt test for hover")).toBe('[![Example image](https://example.com/image.png "My alt test for hover")](https://foo.bar.com)') +}) diff --git a/src/__tests__/repo.test.js b/src/__tests__/repo.test.js index 01703871..65bce9f6 100644 --- a/src/__tests__/repo.test.js +++ b/src/__tests__/repo.test.js @@ -1,9 +1,44 @@ -const { Repo } = require('../components/repo.js') +const { parseRepoUrl, Repo } = require('../components/repo.js') -test('Get correct full URL for a repo', () => { +test("A repo URL can be split correctly", () => { + var url = 'https://github.com/my-username/my-repo-name' + + expect(parseRepoUrl(url)).toBe({ + username: 'my-username', + repoName: 'my-repo-name' + }) +}) + +test("Create a repo instance", () => { + var repo = new Repo('my-username', 'my-repo-name') + + expect(repo.username).toBe('my-username') + expect(repo.repoName).toBe('my-repo-name') +}) + +test("Create a repo instance by key-value pairs", () => { + var repo = new Repo({ username: 'my-username', repoName: 'my-repo-name' }) + + expect(repo.username).toBe('my-username') + expect(repo.repoName).toBe('my-repo-name') +}) + +test('Get Github URL for a repo', () => { var repo = new Repo('my-user', 'my-repo-name') - expect(repo.url()).toBe('https://github.com/my-user/my-repo-name') + expect(repo.githubUrl()).toBe('https://github.com/my-user/my-repo-name') +}) + +test('Get repo-based Github Pages URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.githubPagesUrl()).toBe('https://my-user.github.io/my-repo-name') +}) + +test('Get user-based Github Pages URL for a repo', () => { + var repo = new Repo('my-user') + + expect(repo.githubPagesUrl()).toBe('https://my-user.github.io/') }) test('Get correct full license URL for a repo', () => { @@ -11,3 +46,33 @@ test('Get correct full license URL for a repo', () => { expect(repo.licenseUrl()).toBe('https://github.com/my-user/my-repo-name/blob/master/LICENSE') }) + +test('Get generate template URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.template()).toBe('https://github.com/my-user/my-repo-name/template') +}) + +test('Get tags URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.tags()).toBe('https://github.com/my-user/my-repo-name/tags') +}) + +test('Get releases URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.releases()).toBe('https://github.com/my-user/my-repo-name/releases') +}) + +test('Get forks URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.forks()).toBe('https://github.com/my-user/my-repo-name/network/members') +}) + +test('Get stars URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.stars()).toBe('https://github.com/my-user/my-repo-name/stargazers') +})