Skip to content

Commit

Permalink
Merge pull request #151 from Cyberbeni/more-unit-tests
Browse files Browse the repository at this point in the history
Add more unit tests, add recommended stylistic formatting options
  • Loading branch information
Cyberbeni authored Oct 15, 2024
2 parents 3c4618d + 9b29b20 commit 1d57638
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 29 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: "PR checks"

on: pull_request
on:
pull_request:
push:
branches: master

jobs:
npm-pr-checks:
Expand Down
44 changes: 44 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { SwiftToolInstaller } from '../src/installer'

describe('Version resolving', () => {
test.each([
'2',
'2.4',
'2.4.1',
'<3',
'v2',
'^2',
'~2',
'^2.3',
'~2.4',
'~2.4.0',
'2.x',
])('%s', async (version) => {
const installer = new SwiftToolInstaller('https://github.com/Cyberbeni/install-swift-tool', '', '', version, false)
await installer.resolveVersion()
expect(installer.branch).toBe('v2.4.1')
})
})

describe('Package.resolved v3 parsing', () => {
test('success', async () => {
const installer = new SwiftToolInstaller('https://github.com/nicklockwood/SwiftFormat', '', '', '', false)
const hash = await installer.getCommitHash()
expect(hash).toBe('ab6844edb79a7b88dc6320e6cee0a0db7674dac3')
})
test('error if not found', async () => {
const installer = new SwiftToolInstaller('https://github.com/cpisciotta/xcbeautify', '', '', '', false)
await expect(installer.getCommitHash()).rejects.toThrow()
})
})

describe('Branch/tag resolving', () => {
test.each([
'v2', // branch
'v2.4.1', // tag
])('%s', async (branch) => {
const installer = new SwiftToolInstaller('https://github.com/Cyberbeni/install-swift-tool', '', branch, '', false)
const hash = await installer.getCommitHash()
expect(hash).toBe('7c869f37ca4184c71b60034c7d81e33e1e35d051')
})
})
7 changes: 0 additions & 7 deletions __tests__/main.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import globals from "globals"
import stylistic from "@stylistic/eslint-plugin"
import js from "@eslint/js"
import ts from "typescript-eslint"

Expand All @@ -9,6 +10,7 @@ export default ts.config({
],
ignores: ["src/vendor/**"],
extends: [
stylistic.configs["recommended-flat"],
js.configs.recommended,
...ts.configs.recommended,
],
Expand All @@ -24,10 +26,15 @@ export default ts.config({
}
},
rules: {
"max-len": ["warn", { "code": 140 }],
"indent": ["warn", "tab"],
"quotes": ["warn", "single"],
"semi": ["warn", "never"],
"@stylistic/max-len": ["warn", { "code": 140 }],
"@stylistic/indent": ["warn", "tab"],
"@stylistic/no-tabs": "off",
"@stylistic/quotes": ["warn", "single"],
"@stylistic/semi": ["warn", "never"],
"@stylistic/comma-dangle": ["warn", "always-multiline"],
"@stylistic/brace-style": ["warn", "1tbs"],
"@stylistic/lines-between-class-members": "off",

"@typescript-eslint/no-floating-promises": ["error", { "ignoreVoid": true }]
}
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"devDependencies": {
"@eslint/js": "^9.11.1",
"@stylistic/eslint-plugin": "^2.9.0",
"@tsconfig/node20": "^20.1.4",
"@types/jest": "^29.5.13",
"@types/node": "^22.7.4",
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export async function exec(commandLine: string, args?: string[]): Promise<string
listeners: {
stdout: (data: Buffer) => {
output += data.toString()
}
}
},
},
})
return output.trim()
}
Expand Down
25 changes: 15 additions & 10 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ export class SwiftToolInstaller {
}
}

async getCommitHash(): Promise<string> {
if (this.commit) {
if (this.commit.length != 40) {
throw Error('`commit` should be 40 characters if specified.')
}
return this.commit
} else if (this.branch) {
return (await exec('git', ['ls-remote', '-ht', this.url, `refs/heads/${this.branch}`, `refs/tags/${this.branch}`]))
.substring(0, 40)
} else {
return this.parsePackageResolved()
}
}

uuid = ''
cacheKey = ''
workingDirectory = ''
Expand All @@ -71,16 +85,7 @@ export class SwiftToolInstaller {
}
async createWorkingDirectory(): Promise<void> {
await core.group('Creating working directory', async () => {
if (this.commit) {
if (this.commit.length != 40) {
throw Error('`commit` should be 40 characters if specified.')
}
} else if (this.branch) {
this.commit = (await exec('git', ['ls-remote', '-ht', this.url, `refs/heads/${this.branch}`, `refs/tags/${this.branch}`]))
.substring(0, 40)
} else {
this.commit = this.parsePackageResolved()
}
this.commit = await this.getCommitHash()
this.updateDirectoryNames(await getUuid(this.url, this.commit))
await exec('mkdir', ['-p', this.workingDirectory])
})
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ async function main(): Promise<void> {
await SwiftToolInstaller.install(url, commit, branch, version, useCache)
}

main().catch(error => {
main().catch((error) => {
core.setFailed(error.message)
})
22 changes: 19 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,17 @@
dependencies:
"@sinonjs/commons" "^3.0.0"

"@stylistic/eslint-plugin@^2.9.0":
version "2.9.0"
resolved "https://registry.yarnpkg.com/@stylistic/eslint-plugin/-/eslint-plugin-2.9.0.tgz#5ab3326303915e020ddaf39154290e2800a84bcd"
integrity sha512-OrDyFAYjBT61122MIY1a3SfEgy3YCMgt2vL4eoPmvTwDBwyQhAXurxNQznlRD/jESNfYWfID8Ej+31LljvF7Xg==
dependencies:
"@typescript-eslint/utils" "^8.8.0"
eslint-visitor-keys "^4.1.0"
espree "^10.2.0"
estraverse "^5.3.0"
picomatch "^4.0.2"

"@tsconfig/node20@^20.1.4":
version "20.1.4"
resolved "https://registry.yarnpkg.com/@tsconfig/node20/-/node20-20.1.4.tgz#3457d42eddf12d3bde3976186ab0cd22b85df928"
Expand Down Expand Up @@ -1014,7 +1025,7 @@
semver "^7.6.0"
ts-api-utils "^1.3.0"

"@typescript-eslint/[email protected]":
"@typescript-eslint/[email protected]", "@typescript-eslint/utils@^8.8.0":
version "8.9.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-8.9.0.tgz#748bbe3ea5bee526d9786d9405cf1b0df081c299"
integrity sha512-PKgMmaSo/Yg/F7kIZvrgrWa1+Vwn036CdNUvYFEkYbPwOH4i8xvkaRlu148W3vtheWK9ckKRIz7PBP5oUlkrvQ==
Expand Down Expand Up @@ -1532,7 +1543,7 @@ eslint@^9.11.1:
strip-ansi "^6.0.1"
text-table "^0.2.0"

espree@^10.0.1, espree@^10.1.0:
espree@^10.0.1, espree@^10.1.0, espree@^10.2.0:
version "10.2.0"
resolved "https://registry.yarnpkg.com/espree/-/espree-10.2.0.tgz#f4bcead9e05b0615c968e85f83816bc386a45df6"
integrity sha512-upbkBJbckcCNBDBDXEbuhjbP68n+scUd3k/U2EkyM9nw+I/jPiL4cLF/Al06CF96wRltFda16sxDFrxsI1v0/g==
Expand Down Expand Up @@ -1560,7 +1571,7 @@ esrecurse@^4.3.0:
dependencies:
estraverse "^5.2.0"

estraverse@^5.1.0, estraverse@^5.2.0:
estraverse@^5.1.0, estraverse@^5.2.0, estraverse@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.3.0.tgz#2eea5290702f26ab8fe5370370ff86c965d21123"
integrity sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==
Expand Down Expand Up @@ -2691,6 +2702,11 @@ picomatch@^2.0.4, picomatch@^2.2.3, picomatch@^2.3.1:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==

picomatch@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-4.0.2.tgz#77c742931e8f3b8820946c76cd0c1f13730d1dab"
integrity sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==

pirates@^4.0.4:
version "4.0.6"
resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.6.tgz#3018ae32ecfcff6c29ba2267cbf21166ac1f36b9"
Expand Down

0 comments on commit 1d57638

Please sign in to comment.