From 90af0ffe32b17173863cd27fbd8a1b8c8f0e7582 Mon Sep 17 00:00:00 2001 From: Thibault Friedrich Date: Sun, 10 Mar 2024 19:11:31 -0400 Subject: [PATCH] test: add unit tests --- .../utils/__tests__/removeSection.test.ts | 0 .../contributing/utils/removeSection.ts | 4 --- src/features/license/generateLicense.ts | 2 +- .../utils/__tests__/cleanLicenseName.test.ts | 7 +++++ .../getLicenseContentInReadme.test.ts | 12 +++++++++ .../__tests__/getLicenseFilename.test.ts | 12 +++++++++ .../utils/__tests__/hasProperty.test.ts | 27 +++++++++++++++++++ .../utils/{properties => }/hasProperty.ts | 2 +- .../packageManager/__tests__/npm.test.ts | 27 +++++++++++++++++++ .../packageManager/__tests__/pnpm.test.ts | 27 +++++++++++++++++++ .../packageManager/__tests__/yarn.test.ts | 27 +++++++++++++++++++ src/features/package/packageManager/npm.ts | 4 ++- .../getPackageConfigFilename.test.ts | 10 +++++++ .../{ => utils}/__tests__/setProperty.test.ts | 2 +- .../getPullRequestTemplateFilename.test.ts | 12 +++++++++ 15 files changed, 167 insertions(+), 8 deletions(-) delete mode 100644 src/features/contributing/utils/__tests__/removeSection.test.ts delete mode 100644 src/features/contributing/utils/removeSection.ts create mode 100644 src/features/license/utils/__tests__/cleanLicenseName.test.ts create mode 100644 src/features/license/utils/__tests__/getLicenseContentInReadme.test.ts create mode 100644 src/features/license/utils/__tests__/getLicenseFilename.test.ts create mode 100644 src/features/license/utils/__tests__/hasProperty.test.ts rename src/features/license/utils/{properties => }/hasProperty.ts (57%) create mode 100644 src/features/package/packageManager/__tests__/npm.test.ts create mode 100644 src/features/package/packageManager/__tests__/pnpm.test.ts create mode 100644 src/features/package/packageManager/__tests__/yarn.test.ts create mode 100644 src/features/package/utils/__tests__/getPackageConfigFilename.test.ts rename src/features/package/{ => utils}/__tests__/setProperty.test.ts (97%) create mode 100644 src/features/pullRequestTemplate/utils/__tests__/getPullRequestTemplateFilename.test.ts diff --git a/src/features/contributing/utils/__tests__/removeSection.test.ts b/src/features/contributing/utils/__tests__/removeSection.test.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/features/contributing/utils/removeSection.ts b/src/features/contributing/utils/removeSection.ts deleted file mode 100644 index 26fb530..0000000 --- a/src/features/contributing/utils/removeSection.ts +++ /dev/null @@ -1,4 +0,0 @@ -const removeSection = (content: string, section: string) => - content.replace(new RegExp(`\\{\\{ ${section} \\}\\}`, 'g'), '') - -export default removeSection diff --git a/src/features/license/generateLicense.ts b/src/features/license/generateLicense.ts index 02ed3eb..bee8aab 100644 --- a/src/features/license/generateLicense.ts +++ b/src/features/license/generateLicense.ts @@ -7,7 +7,7 @@ import * as packageConfig from '../package' import * as context from '../../context' import listLicenseFiles, { getFullPath } from './utils/listLicenseFiles' import cleanLicenseName from './utils/cleanLicenseName' -import hasProperty from './utils/properties/hasProperty' +import hasProperty from './utils/hasProperty' import * as readme from '../readme' import printTerminal from '../../services/terminal/printTerminal' import setVariable from '../../services/template/setVariable' diff --git a/src/features/license/utils/__tests__/cleanLicenseName.test.ts b/src/features/license/utils/__tests__/cleanLicenseName.test.ts new file mode 100644 index 0000000..62938ce --- /dev/null +++ b/src/features/license/utils/__tests__/cleanLicenseName.test.ts @@ -0,0 +1,7 @@ +import cleanLicenseName from '../cleanLicenseName' + +describe('cleanLicenseName', () => { + it('should remove .txt and convert to uppercase', () => { + expect(cleanLicenseName('mit.txt')).toEqual('MIT') + }) +}) diff --git a/src/features/license/utils/__tests__/getLicenseContentInReadme.test.ts b/src/features/license/utils/__tests__/getLicenseContentInReadme.test.ts new file mode 100644 index 0000000..a1dc7e6 --- /dev/null +++ b/src/features/license/utils/__tests__/getLicenseContentInReadme.test.ts @@ -0,0 +1,12 @@ +import getLicenseContentInReadme from '../getLicenseContentInReadme' + +describe('getLicenseContentInReadme', () => { + it('should return the content of the license section in the readme', () => { + const expectedLicenseContent = + 'This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.' + + const licenseContent = getLicenseContentInReadme('MIT') + + expect(licenseContent).toEqual(expectedLicenseContent) + }) +}) diff --git a/src/features/license/utils/__tests__/getLicenseFilename.test.ts b/src/features/license/utils/__tests__/getLicenseFilename.test.ts new file mode 100644 index 0000000..0c5b1d4 --- /dev/null +++ b/src/features/license/utils/__tests__/getLicenseFilename.test.ts @@ -0,0 +1,12 @@ +import getLicenseFilename from '../getLicenseFilename' + +describe('getLicenseFilename', () => { + it('should return the path to the license file', () => { + const repositoryPath = '/path/to/repo' + const expectedFilename = '/path/to/repo/LICENSE' + + const filename = getLicenseFilename(repositoryPath) + + expect(filename).toBe(expectedFilename) + }) +}) diff --git a/src/features/license/utils/__tests__/hasProperty.test.ts b/src/features/license/utils/__tests__/hasProperty.test.ts new file mode 100644 index 0000000..222efb2 --- /dev/null +++ b/src/features/license/utils/__tests__/hasProperty.test.ts @@ -0,0 +1,27 @@ +import hasProperty from '../hasProperty' + +describe('hasProperty', () => { + it('should return true', () => { + const content = 'foo {{var}} bar' + + expect(hasProperty(content, 'var')).toBe(true) + }) + + it('should return true when template has space', () => { + const content = 'foo {{ var }} bar' + + expect(hasProperty(content, 'var')).toBe(true) + }) + + it('should return false when no variable', () => { + const content = 'foo bar' + + expect(hasProperty(content, 'var')).toBe(false) + }) + + it('should return false when variable with wrong name', () => { + const content = 'foo {{var2}} bar' + + expect(hasProperty(content, 'var')).toBe(false) + }) +}) diff --git a/src/features/license/utils/properties/hasProperty.ts b/src/features/license/utils/hasProperty.ts similarity index 57% rename from src/features/license/utils/properties/hasProperty.ts rename to src/features/license/utils/hasProperty.ts index ca8d2a2..6fb3af1 100644 --- a/src/features/license/utils/properties/hasProperty.ts +++ b/src/features/license/utils/hasProperty.ts @@ -1,4 +1,4 @@ const hasProperty = (content: string, property: string) => - content.includes(`{{ ${property} }}`) + Boolean(content.match(new RegExp(`{{[ ]*${property}[ ]*}}`))) export default hasProperty diff --git a/src/features/package/packageManager/__tests__/npm.test.ts b/src/features/package/packageManager/__tests__/npm.test.ts new file mode 100644 index 0000000..de7e3ff --- /dev/null +++ b/src/features/package/packageManager/__tests__/npm.test.ts @@ -0,0 +1,27 @@ +import npm from '../npm' + +describe('npm', () => { + describe('getConfigFile', () => { + it('should return package-lock.yaml', () => { + expect(npm.getConfigFile()).toBe('package-lock.yaml') + }) + }) + + describe('getCommand', () => { + it('should return npm install', () => { + expect(npm.getCommand('install')).toBe('npm install') + }) + + it('should return npm test', () => { + expect(npm.getCommand('test')).toBe('npm test') + }) + + it('should return npm start', () => { + expect(npm.getCommand('start')).toBe('npm start') + }) + + it('should return npm run format', () => { + expect(npm.getCommand('format')).toBe('npm run format') + }) + }) +}) diff --git a/src/features/package/packageManager/__tests__/pnpm.test.ts b/src/features/package/packageManager/__tests__/pnpm.test.ts new file mode 100644 index 0000000..34f96b6 --- /dev/null +++ b/src/features/package/packageManager/__tests__/pnpm.test.ts @@ -0,0 +1,27 @@ +import pnpm from '../pnpm' + +describe('pnpm', () => { + describe('getConfigFile', () => { + it('should return pnpm-lock.yaml', () => { + expect(pnpm.getConfigFile()).toBe('pnpm-lock.yaml') + }) + }) + + describe('getCommand', () => { + it('should return pnpm install', () => { + expect(pnpm.getCommand('install')).toBe('pnpm install') + }) + + it('should return pnpm test', () => { + expect(pnpm.getCommand('test')).toBe('pnpm test') + }) + + it('should return pnpm start', () => { + expect(pnpm.getCommand('start')).toBe('pnpm start') + }) + + it('should return pnpm format', () => { + expect(pnpm.getCommand('format')).toBe('pnpm format') + }) + }) +}) diff --git a/src/features/package/packageManager/__tests__/yarn.test.ts b/src/features/package/packageManager/__tests__/yarn.test.ts new file mode 100644 index 0000000..37ae50b --- /dev/null +++ b/src/features/package/packageManager/__tests__/yarn.test.ts @@ -0,0 +1,27 @@ +import yarn from '../yarn' + +describe('yarn', () => { + describe('getConfigFile', () => { + it('should return yarn.lock', () => { + expect(yarn.getConfigFile()).toBe('yarn.lock') + }) + }) + + describe('getCommand', () => { + it('should return yarn install', () => { + expect(yarn.getCommand('install')).toBe('yarn install') + }) + + it('should return yarn test', () => { + expect(yarn.getCommand('test')).toBe('yarn test') + }) + + it('should return yarn start', () => { + expect(yarn.getCommand('start')).toBe('yarn start') + }) + + it('should return yarn format', () => { + expect(yarn.getCommand('format')).toBe('yarn format') + }) + }) +}) diff --git a/src/features/package/packageManager/npm.ts b/src/features/package/packageManager/npm.ts index 9fc91f2..08e9062 100644 --- a/src/features/package/packageManager/npm.ts +++ b/src/features/package/packageManager/npm.ts @@ -1,9 +1,11 @@ import PackageManager from './PackageManager' +const commandsWithoutRun = ['start', 'test', 'install'] + const npm: PackageManager = { getConfigFile: () => 'package-lock.yaml', getCommand: (script: string) => - ['start', 'test'].includes(script) ? `npm ${script}` : `pnpm run ${script}`, + commandsWithoutRun.includes(script) ? `npm ${script}` : `npm run ${script}`, } export default npm diff --git a/src/features/package/utils/__tests__/getPackageConfigFilename.test.ts b/src/features/package/utils/__tests__/getPackageConfigFilename.test.ts new file mode 100644 index 0000000..dd22a97 --- /dev/null +++ b/src/features/package/utils/__tests__/getPackageConfigFilename.test.ts @@ -0,0 +1,10 @@ +import getPackageConfigFilename from '../getPackageConfigFilename' + +describe('getPackageConfigFilename', () => { + it('should return the package.json path', () => { + const repositoryPath = '/path/to/repository' + const expectedFilename = '/path/to/repository/package.json' + + expect(getPackageConfigFilename(repositoryPath)).toBe(expectedFilename) + }) +}) diff --git a/src/features/package/__tests__/setProperty.test.ts b/src/features/package/utils/__tests__/setProperty.test.ts similarity index 97% rename from src/features/package/__tests__/setProperty.test.ts rename to src/features/package/utils/__tests__/setProperty.test.ts index 4d6c3f2..9309677 100644 --- a/src/features/package/__tests__/setProperty.test.ts +++ b/src/features/package/utils/__tests__/setProperty.test.ts @@ -1,4 +1,4 @@ -import setProperty from '../utils/setProperty' +import setProperty from '../setProperty' const formattedContent = (content: object) => JSON.stringify(content, null, 2) diff --git a/src/features/pullRequestTemplate/utils/__tests__/getPullRequestTemplateFilename.test.ts b/src/features/pullRequestTemplate/utils/__tests__/getPullRequestTemplateFilename.test.ts new file mode 100644 index 0000000..030b419 --- /dev/null +++ b/src/features/pullRequestTemplate/utils/__tests__/getPullRequestTemplateFilename.test.ts @@ -0,0 +1,12 @@ +import getPullRequestTemplateFilename from '../getPullRequestTemplateFilename' + +describe('getPullRequestTemplateFilename', () => { + it('should return the path to the pull request template file', () => { + const repositoryPath = '/path/to/repo' + const expectedFilename = '/path/to/repo/.github/PULL_REQUEST_TEMPLATE.md' + + const filename = getPullRequestTemplateFilename(repositoryPath) + + expect(filename).toBe(expectedFilename) + }) +})