From 2cd934a310d9536d8f43c2eda662da68ffd60022 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 4 Apr 2023 12:21:42 +1000 Subject: [PATCH 1/5] chore(ci): add auto-release --- .github/dependabot.yml | 16 +++++ .github/workflows/test-and-release.yml | 61 ++++++++++++++++++ package.json | 85 +++++++++++++++++++++++++- test.js | 14 +++++ 4 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/test-and-release.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..f468993 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,16 @@ +version: 2 +updates: + - package-ecosystem: 'github-actions' + directory: '/' + schedule: + interval: 'daily' + commit-message: + prefix: 'chore' + include: 'scope' + - package-ecosystem: 'npm' + directory: '/' + schedule: + interval: 'daily' + commit-message: + prefix: 'chore' + include: 'scope' diff --git a/.github/workflows/test-and-release.yml b/.github/workflows/test-and-release.yml new file mode 100644 index 0000000..c379ea8 --- /dev/null +++ b/.github/workflows/test-and-release.yml @@ -0,0 +1,61 @@ +name: Test & Maybe Release +on: [push, pull_request] +jobs: + test: + strategy: + fail-fast: false + matrix: + node: [16.x, 18.x, lts/*, current] + os: [macos-latest, ubuntu-latest, windows-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node }} + uses: actions/setup-node@v3.5.1 + with: + node-version: ${{ matrix.node }} + - name: Install Dependencies + run: | + npm install --no-progress + - name: Run tests + run: | + npm config set script-shell bash + npm run test:ci + release: + name: Release + needs: test + runs-on: ubuntu-latest + if: github.event_name == 'push' && github.ref == 'refs/heads/master' + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Setup Node.js + uses: actions/setup-node@v3.5.1 + with: + node-version: lts/* + - name: Install dependencies + run: | + npm install --no-progress --no-package-lock --no-save + - name: Build + run: | + npm run build + - name: Install plugins + run: | + npm install \ + @semantic-release/commit-analyzer \ + conventional-changelog-conventionalcommits \ + @semantic-release/release-notes-generator \ + @semantic-release/npm \ + @semantic-release/github \ + @semantic-release/git \ + @semantic-release/changelog \ + --no-progress --no-package-lock --no-save + - name: Release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: npx semantic-release + diff --git a/package.json b/package.json index d1b31a5..546fc10 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,8 @@ "description": "Turn a `git log` into a stream of commit objects", "main": "commit-stream.js", "scripts": { + "build": "true", + "test:ci": "npm run test", "test": "node test.js" }, "repository": { @@ -19,10 +21,91 @@ "devDependencies": { "list-stream": "~1.0.0", "split2": "~1.0.0", - "tape": "~4.0.1" + "tape": "^5.6.3" }, "dependencies": { "strip-ansi": "^6.0.1", "through2": "~2.0.0" + }, + "release": { + "branches": [ + "main" + ], + "plugins": [ + [ + "@semantic-release/commit-analyzer", + { + "preset": "conventionalcommits", + "releaseRules": [ + { + "breaking": true, + "release": "major" + }, + { + "revert": true, + "release": "patch" + }, + { + "type": "feat", + "release": "minor" + }, + { + "type": "fix", + "release": "patch" + }, + { + "type": "chore", + "release": "patch" + }, + { + "type": "docs", + "release": "patch" + }, + { + "type": "test", + "release": "patch" + }, + { + "scope": "no-release", + "release": false + } + ] + } + ], + [ + "@semantic-release/release-notes-generator", + { + "preset": "conventionalcommits", + "presetConfig": { + "types": [ + { + "type": "feat", + "section": "Features" + }, + { + "type": "fix", + "section": "Bug Fixes" + }, + { + "type": "chore", + "section": "Trivial Changes" + }, + { + "type": "docs", + "section": "Trivial Changes" + }, + { + "type": "test", + "section": "Tests" + } + ] + } + } + ], + "@semantic-release/changelog", + "@semantic-release/npm", + "@semantic-release/github", + "@semantic-release/git" + ] } } diff --git a/test.js b/test.js index be1b9bf..7c3392d 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,7 @@ const through2 = require('through2') , commitStream = require('./') , spawn = require('child_process').spawn + , exec = require('child_process').exec , test = require('tape') , split2 = require('split2') , listStream = require('list-stream') @@ -11,6 +12,19 @@ function gitToList (gitCmd, callback) { child.stdout.pipe(split2()).pipe(commitStream()).pipe(listStream.obj(callback)) } +test('git is runnable', function (t) { + exec('git version', (error, stdout, stderr) => { + if (error) { + t.fail(`command failed: ${error}`) + } else if (stderr) { + t.fail(`command output to stderr: ${stderr}`) + } else { + t.match(stdout, /^git version 2/) + } + t.end() + }) +}) + test('current plain commit log', function (t) { gitToList('git log', function (err, list) { From 9eed6ba8b66efbecc6cce95259154e6fb6ba6344 Mon Sep 17 00:00:00 2001 From: Mohammed Keyvanzadeh Date: Sat, 1 Apr 2023 20:21:54 +0330 Subject: [PATCH 2/5] feat!: refactor, format, and update - Rewrite to ESM. - Refactor the JavaScript files. - Update dependencies and developer dependencies to their latest versions. - Format files with Prettier. - Add `package-lock.json` to `.gitignore`. - Add missing backtick in `README.md`. --- .gitignore | 1 + LICENSE.md | 6 +- README.md | 31 +++--- commit-stream.js | 121 ++++++++++++--------- package.json | 9 +- test.js | 266 +++++++++++++++++++++++++++-------------------- 6 files changed, 247 insertions(+), 187 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..d5f19d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +package-lock.json diff --git a/LICENSE.md b/LICENSE.md index 43f7153..fceb2e2 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,8 +1,6 @@ -The MIT License (MIT) -===================== +# The MIT License (MIT) -Copyright (c) 2015 Rod Vagg ---------------------------- +## Copyright (c) 2015 Rod Vagg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 29e5571..db10c33 100644 --- a/README.md +++ b/README.md @@ -16,16 +16,15 @@ Intended to be used on a standard log format (not a simplified one using one of Typical usage: ```js -var spawn = require('child_process').spawn - , split2 = require('split2') - , listStream = require('list-stream') +import { spawn } from 'node:child_process' +import split2 from 'split2' +import listStream from 'list-stream' -spawn('bash', [ '-c', 'git log' ]) +spawn('bash', ['-c', 'git log']) .stdout.pipe(split2()) // break up by newline characters .pipe(commitStream('rvagg', 'commit-stream')) .pipe(listStream.obj(onCommitList)) - function onCommitList (err, list) { // list is an array of objects } @@ -33,22 +32,22 @@ function onCommitList (err, list) { Commit object properties: -* `sha`: the full commit sha -* `author`: an object representing the author of the commit +- `sha`: the full commit sha +- `author`: an object representing the author of the commit - `name`: the name of the committer - `email`: the email of the committer -* `authors`: a list of such objects representing the authors of the commit, supporting multiple authors through `Co-authored-by:` -* `authorDate`: a string representing the date of the original commit by the author (never change) -* `commitDate`: a string representing the date of the last change of the commit -* `summary`: the one-line summary of the commit -* `description`: the free-form text content of the summary, minus specific metadata lines -* `reviewers`: an array containing objects with `name` and `email` properties if the commit metadata contains reviewers in a `Reviewed-By: Foo Bar ` format. -* `changes`: if `--stat` data is included in the git log, an object containing change stats: +- `authors`: a list of such objects representing the authors of the commit, supporting multiple authors through `Co-authored-by:` +- `authorDate`: a string representing the date of the original commit by the author (never change) +- `commitDate`: a string representing the date of the last change of the commit +- `summary`: the one-line summary of the commit +- `description`: the free-form text content of the summary, minus specific metadata lines +- `reviewers`: an array containing objects with `name` and `email` properties if the commit metadata contains reviewers in a `Reviewed-By: Foo Bar ` format. +- `changes`: if `--stat` data is included in the git log, an object containing change stats: - `files`: the number of files changed - `insertions`: the number of new lines inserted - `deletions`: the number of old lines removed -* `prUrl`: a URL pointing to a pull-request where this change was made if the commit metadata contains a `PR-URL: https://github.com/user/project/pull/XX` line. Note that whatever proceeds the `PR-URL: ` string will be collected in this property; one exception being that if a shortened `#XX` version is found and you have supplied `defaultGitHubUser` and `defaultGitHubProject` arguments to the constructor then a full GitHub pull-request will be reconstituted in its place. -* `ghUser`, `ghProject, `ghIssue`: if a proper GitHub pull request is found for the `prUrl` property (including shortened `#XX` ones), these properties will be added to point to the original user, project and issue (pull-request) for this change, as extracted from the URL. +- `prUrl`: a URL pointing to a pull-request where this change was made if the commit metadata contains a `PR-URL: https://github.com/user/project/pull/XX` line. Note that whatever proceeds the `PR-URL: ` string will be collected in this property; one exception being that if a shortened `#XX` version is found and you have supplied `defaultGitHubUser` and `defaultGitHubProject` arguments to the constructor then a full GitHub pull-request will be reconstituted in its place. +- `ghUser`, `ghProject`, `ghIssue`: if a proper GitHub pull request is found for the `prUrl` property (including shortened `#XX` ones), these properties will be added to point to the original user, project and issue (pull-request) for this change, as extracted from the URL. ## License diff --git a/commit-stream.js b/commit-stream.js index 62d9d87..583a64b 100644 --- a/commit-stream.js +++ b/commit-stream.js @@ -1,75 +1,91 @@ -const through2 = require('through2') - , stripAnsi = require('strip-ansi') +import through2 from 'through2' +import stripAnsi from 'strip-ansi' - -module.exports = commitStream - - -function commitStream (ghUser, ghProject) { - var commit +export default function commitStream (ghUser, ghProject) { + let commit return through2.obj(onLine, onEnd) function addLine (line) { line = stripAnsi(line) - if (!line) - return + if (line.length === 0) return - var old, m + let old, m if (/^commit \w+$/.test(line)) { old = commit - commit = { - sha: line.split(' ')[1] + commit = { sha: line.split(' ')[1] } + } else if ((m = line.match(/^CommitDate:\s+(.*)$/)) !== null) { + if (commit === undefined || commit === null) { + throw new Error('Something unexpected occurred') } - } else if (m = line.match(/^CommitDate:\s+(.*)$/)) { - if (!commit) - throw new Error('wut?') commit.commitDate = m[1].trim() - } else if (m = line.match(/^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i)) { - if (!commit) - throw new Error('wut?') - if (!commit.authors) + } else if ( + (m = line.match( + /^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i + )) !== null + ) { + if (commit === undefined || commit === null) { + throw new Error('Something unexpected occurred') + } + if (commit.authors === undefined || commit.authors === null) { commit.authors = [] + } commit.authors.push({ name: m[1], email: m[2] }) - } else if (m = line.match(/^AuthorDate:\s+(.*)$/)) { - if (!commit) - throw new Error('wut?') - commit.authorDate = m[1].trim() - } else if (m = line.match(/^Date:\s+(.*)$/)) { - if (!commit) - throw new Error('wut?') + } else if ((m = line.match(/^(?:AuthorDate|Date):\s+(.*)$/)) !== null) { + if (commit === undefined || commit === null) { + throw new Error('Something unexpected occurred') + } commit.authorDate = m[1].trim() - } else if (m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) { - if (!commit.reviewers) + } else if ( + (m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) !== + null + ) { + if (commit.reviewers === undefined || commit.reviewers === null) { commit.reviewers = [] + } commit.reviewers.push({ name: m[1], email: m[2] }) - } else if (m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/)) { + } else if ((m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/)) !== null) { commit.prUrl = m[1] - if (ghUser && ghProject && (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/))) { - commit.prUrl = 'https://github.com/' + ghUser + '/' + ghProject + '/pull/' + m[1] + if ( + typeof ghUser === 'string' && + ghUser.length !== 0 && + typeof ghProject === 'string' && + ghProject.length !== 0 && + (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/)) !== null + ) { + commit.prUrl = `https://github.com/${ghUser}/${ghProject}/pull/${m[1]}` } - if (m = commit.prUrl.match(/^(https?:\/\/.+\/([^\/]+)\/([^\/]+))\/\w+\/(\d+)(\/)?$/i)) { - commit.ghIssue = +m[4] - commit.ghUser = m[2] + if ( + (m = commit.prUrl.match( + /^(https?:\/\/.+\/([^/]+)\/([^/]+))\/\w+\/(\d+)(\/)?$/i + )) !== null + ) { + commit.ghIssue = parseInt(m[4]) + commit.ghUser = m[2] commit.ghProject = m[3] } - } else if (/^ /.test(line) && (line = line.trim()).length) { - if (!commit.summary) { + } else if (/^ {4}/.test(line) && (line = line.trim()).length !== 0) { + if (commit.summary === undefined || commit.summary === null) { commit.summary = line } else { - if (!commit.description) + if (commit.description === undefined || commit.description === null) { commit.description = [] + } commit.description.push(line) } - } else if (/^ [^ ]/.test(line) && (line = line.trim()).length) { - if (m = line.match(/^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/)) { - commit.changes = { - files : Number(m[1]) - , insertions : Number(m[2] || 0) - , deletions : Number(m[3] || 0) - } + } else if ( + /^ [^ ]/.test(line) && + (line = line.trim()).length !== 0 && + (m = line.match( + /^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/ + )) !== null + ) { + commit.changes = { + files: parseInt(m[1]), + insertions: parseInt(m[2] ?? 0), + deletions: parseInt(m[3] ?? 0) } } @@ -77,19 +93,24 @@ function commitStream (ghUser, ghProject) { } function onLine (line, _, callback) { - var commit = addLine(line) - if (commit && commit.authors && commit.authors.length > 0) + const commit = addLine(line) + + if (commit?.authors?.length > 0) { commit.author = commit.authors[0] - if (commit) + this.push(commit) + } + callback() } function onEnd (callback) { - if (commit && commit.authors && commit.authors.length > 0) + if (commit?.authors?.length > 0) { commit.author = commit.authors[0] - if (commit) + this.push(commit) + } + callback() } } diff --git a/package.json b/package.json index 546fc10..5019b28 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.1.0", "description": "Turn a `git log` into a stream of commit objects", "main": "commit-stream.js", + "type": "module", "scripts": { "build": "true", "test:ci": "npm run test", @@ -19,13 +20,13 @@ }, "homepage": "https://github.com/rvagg/commit-stream", "devDependencies": { - "list-stream": "~1.0.0", - "split2": "~1.0.0", + "list-stream": "^2.1.0", + "split2": "^4.2.0", "tape": "^5.6.3" }, "dependencies": { - "strip-ansi": "^6.0.1", - "through2": "~2.0.0" + "strip-ansi": "^7.0.1", + "through2": "^4.0.2" }, "release": { "branches": [ diff --git a/test.js b/test.js index 7c3392d..16443e5 100644 --- a/test.js +++ b/test.js @@ -1,18 +1,18 @@ -const through2 = require('through2') - , commitStream = require('./') - , spawn = require('child_process').spawn - , exec = require('child_process').exec - , test = require('tape') - , split2 = require('split2') - , listStream = require('list-stream') - +import commitStream from './commit-stream.js' +import { spawn, exec } from 'node:child_process' +import test from 'tape' +import split2 from 'split2' +import listStream from 'list-stream' function gitToList (gitCmd, callback) { - var child = spawn('bash', [ '-c', gitCmd ]) - child.stdout.pipe(split2()).pipe(commitStream()).pipe(listStream.obj(callback)) + const child = spawn('bash', ['-c', gitCmd]) + child.stdout + .pipe(split2()) + .pipe(commitStream()) + .pipe(listStream.obj(callback)) } -test('git is runnable', function (t) { +test('git is runnable', (t) => { exec('git version', (error, stdout, stderr) => { if (error) { t.fail(`command failed: ${error}`) @@ -25,117 +25,157 @@ test('git is runnable', function (t) { }) }) - -test('current plain commit log', function (t) { - gitToList('git log', function (err, list) { +test('current plain commit log', (t) => { + gitToList('git log', (err, list) => { t.error(err, 'no error') - t.ok(list && list.length > 1, 'got a list') - - t.deepEqual(list[list.length - 9], { - author : { email: 'ralphtheninja@riseup.net', name: 'Lars-Magnus Skog' } - , authors : [ { email: 'ralphtheninja@riseup.net', name: 'Lars-Magnus Skog' } ] - , authorDate : 'Tue Feb 9 15:46:46 2016 +0100' - , description : [ - 'Fixes: https://github.com/rvagg/changelog-maker/issues/35' - ] - , ghIssue : 1 - , ghProject : 'commit-stream' - , ghUser : 'rvagg' - , prUrl : 'https://github.com/rvagg/commit-stream/pull/1' - , sha : '1bcfd072fd74399808fbcd5cf31ce5342dd6d70c' - , summary : 'process: this should not match PR-URL' - , reviewers : [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - }, 'got correct ninth commit') - - t.deepEqual(list[list.length - 4], { - author: { email: 'rod@vagg.org', name: 'Rod Vagg' } - , authors: [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - , authorDate: 'Fri Apr 17 11:16:51 2015 +1000' - , sha: 'f92b93c3c7175b07f847dd45058b121cea6b3a20' - , summary: 'deleted package.json line' - }, 'got correct fourth commit') - - t.deepEqual(list[list.length - 3], { - author: { email: 'rod@vagg.org', name: 'Rod Vagg' } - , authors: [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - , authorDate: 'Fri Apr 17 11:13:06 2015 +1000' - , description: [ 'comment', 'Reviewed-By: Nobody' ] - , sha: 'db34ce2af09a6a9fb70241d43965a2bc48b90b4c' - , summary: 'squished package.json' - }, 'got correct third commit') - - t.deepEqual(list[list.length - 2], { - author : { email: 'rod@vagg.org', name: 'Rod Vagg' } - , authors : [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - , authorDate: 'Fri Apr 17 10:52:16 2015 +1000' - , description : [ - 'Some extra summary information here' - , 'newline' - , 'Fixes: https://github.com/rvagg/commit-stream/issues/0' - ] - , ghIssue : 0 - , ghProject : 'commit-stream' - , ghUser : 'rvagg' - , prUrl : 'https://github.com/rvagg/commit-stream/pulls/0' - , reviewers : [ { email: 'roger@vagg.org', name: 'Roger Vagg' } ] - , sha : '6275758f597ae579202fbe4eccca1407f2c67ac1' - , summary : 'added test.js' - }, 'got correct second commit') - - t.deepEqual(list[list.length - 1], { - sha : 'd94841274e2979e7758413a0f48fa37560d0dde6' - , authorDate: 'Thu Apr 16 20:49:21 2015 +1000' - , author : { name: 'Rod Vagg', email: 'rod@vagg.org' } - , authors: [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - , summary: 'make it so' - }, 'got correct first commit') - - t.deepEqual(list[list.length - 16], { - sha : list[list.length - 16].sha // unknown at time of writing :) - , authorDate: 'Tue Jun 12 23:41:35 2018 +0200' - , author : { name: 'Anna Henningsen', email: 'anna@addaleax.net' } - , authors: [ - { name: 'Anna Henningsen', email: 'anna@addaleax.net' } - , { name: 'nobody', email: 'nobody@nowhere' } - ] - , summary: 'add support for co-authored-by' - }, 'got correct co-authored-by commit') + t.ok(list?.length > 1, 'got a list') + + t.deepEqual( + list[list.length - 9], + { + author: { email: 'ralphtheninja@riseup.net', name: 'Lars-Magnus Skog' }, + authors: [ + { email: 'ralphtheninja@riseup.net', name: 'Lars-Magnus Skog' } + ], + authorDate: 'Tue Feb 9 15:46:46 2016 +0100', + description: [ + 'Fixes: https://github.com/rvagg/changelog-maker/issues/35' + ], + ghIssue: 1, + ghProject: 'commit-stream', + ghUser: 'rvagg', + prUrl: 'https://github.com/rvagg/commit-stream/pull/1', + sha: '1bcfd072fd74399808fbcd5cf31ce5342dd6d70c', + summary: 'process: this should not match PR-URL', + reviewers: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }] + }, + 'got correct ninth commit' + ) + + t.deepEqual( + list[list.length - 4], + { + author: { email: 'rod@vagg.org', name: 'Rod Vagg' }, + authors: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }], + authorDate: 'Fri Apr 17 11:16:51 2015 +1000', + sha: 'f92b93c3c7175b07f847dd45058b121cea6b3a20', + summary: 'deleted package.json line' + }, + 'got correct fourth commit' + ) + + t.deepEqual( + list[list.length - 3], + { + author: { email: 'rod@vagg.org', name: 'Rod Vagg' }, + authors: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }], + authorDate: 'Fri Apr 17 11:13:06 2015 +1000', + description: ['comment', 'Reviewed-By: Nobody'], + sha: 'db34ce2af09a6a9fb70241d43965a2bc48b90b4c', + summary: 'squished package.json' + }, + 'got correct third commit' + ) + + t.deepEqual( + list[list.length - 2], + { + author: { email: 'rod@vagg.org', name: 'Rod Vagg' }, + authors: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }], + authorDate: 'Fri Apr 17 10:52:16 2015 +1000', + description: [ + 'Some extra summary information here', + 'newline', + 'Fixes: https://github.com/rvagg/commit-stream/issues/0' + ], + ghIssue: 0, + ghProject: 'commit-stream', + ghUser: 'rvagg', + prUrl: 'https://github.com/rvagg/commit-stream/pulls/0', + reviewers: [{ email: 'roger@vagg.org', name: 'Roger Vagg' }], + sha: '6275758f597ae579202fbe4eccca1407f2c67ac1', + summary: 'added test.js' + }, + 'got correct second commit' + ) + + t.deepEqual( + list[list.length - 1], + { + sha: 'd94841274e2979e7758413a0f48fa37560d0dde6', + authorDate: 'Thu Apr 16 20:49:21 2015 +1000', + author: { name: 'Rod Vagg', email: 'rod@vagg.org' }, + authors: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }], + summary: 'make it so' + }, + 'got correct first commit' + ) + + t.deepEqual( + list[list.length - 16], + { + sha: list[list.length - 16].sha, // unknown at time of writing :) + authorDate: 'Tue Jun 12 23:41:35 2018 +0200', + author: { name: 'Anna Henningsen', email: 'anna@addaleax.net' }, + authors: [ + { name: 'Anna Henningsen', email: 'anna@addaleax.net' }, + { name: 'nobody', email: 'nobody@nowhere' } + ], + summary: 'add support for co-authored-by' + }, + 'got correct co-authored-by commit' + ) t.end() }) }) - -test('current commit log with changes', function (t) { - gitToList('git log --stat', function (err, list) { +test('current commit log with changes', (t) => { + gitToList('git log --stat', (err, list) => { t.error(err, 'no errors') - t.ok(list && list.length > 0, 'got a list') - - t.deepEqual(list[list.length - 4].changes, { - files : 1 - , insertions : 0 - , deletions : 1 - }, 'got correct second commit changes') - - t.deepEqual(list[list.length - 3].changes, { - files : 1 - , insertions : 1 - , deletions : 28 - }, 'got correct second commit changes') - - t.deepEqual(list[list.length - 2].changes, { - files : 1 - , insertions : 49 - , deletions : 0 - }, 'got correct second commit changes') - - t.deepEqual(list[list.length - 1].changes, { - files : 1 - , insertions : 28 - , deletions : 0 - }, 'got correct first commit changes') + t.ok(list?.length > 0, 'got a list') + + t.deepEqual( + list[list.length - 4].changes, + { + files: 1, + insertions: 0, + deletions: 1 + }, + 'got correct second commit changes' + ) + + t.deepEqual( + list[list.length - 3].changes, + { + files: 1, + insertions: 1, + deletions: 28 + }, + 'got correct second commit changes' + ) + + t.deepEqual( + list[list.length - 2].changes, + { + files: 1, + insertions: 49, + deletions: 0 + }, + 'got correct second commit changes' + ) + + t.deepEqual( + list[list.length - 1].changes, + { + files: 1, + insertions: 28, + deletions: 0 + }, + 'got correct first commit changes' + ) t.end() }) From 61a03af4c5885795d0537d1805b541461074400e Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 4 Apr 2023 13:02:21 +1000 Subject: [PATCH 3/5] fix: use pipeline to catch errors --- package.json | 1 + test.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 5019b28..59100f5 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "homepage": "https://github.com/rvagg/commit-stream", "devDependencies": { "list-stream": "^2.1.0", + "readable-stream": "^4.3.0", "split2": "^4.2.0", "tape": "^5.6.3" }, diff --git a/test.js b/test.js index 16443e5..d3b221e 100644 --- a/test.js +++ b/test.js @@ -3,13 +3,17 @@ import { spawn, exec } from 'node:child_process' import test from 'tape' import split2 from 'split2' import listStream from 'list-stream' +import { pipeline } from 'readable-stream' function gitToList (gitCmd, callback) { const child = spawn('bash', ['-c', gitCmd]) - child.stdout - .pipe(split2()) - .pipe(commitStream()) - .pipe(listStream.obj(callback)) + pipeline( + child.stdout, + split2(), + commitStream(), + listStream.obj(callback), + () => {} + ) } test('git is runnable', (t) => { From 3cdbbfb73af54771ebdcbd0acb98d6202b8179d7 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 4 Apr 2023 13:27:29 +1000 Subject: [PATCH 4/5] fix: check for and handle process erorrs --- package.json | 1 + test.js | 39 ++++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 59100f5..379c76b 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "homepage": "https://github.com/rvagg/commit-stream", "devDependencies": { + "bl": "^6.0.1", "list-stream": "^2.1.0", "readable-stream": "^4.3.0", "split2": "^4.2.0", diff --git a/test.js b/test.js index d3b221e..51e84c3 100644 --- a/test.js +++ b/test.js @@ -4,9 +4,20 @@ import test from 'tape' import split2 from 'split2' import listStream from 'list-stream' import { pipeline } from 'readable-stream' +import bl from 'bl' -function gitToList (gitCmd, callback) { +function gitToList (t, gitCmd, callback) { const child = spawn('bash', ['-c', gitCmd]) + child.stderr.pipe(bl((_, out) => { + t.strictEqual(out.toString(), '') + })) + child.on('error', (err) => { + t.error(err, 'error from child') + }) + child.on('close', (code) => { + t.strictEqual(code, 0, 'exit code zero') + setImmediate(t.end.bind(t)) + }) pipeline( child.stdout, split2(), @@ -30,10 +41,13 @@ test('git is runnable', (t) => { }) test('current plain commit log', (t) => { - gitToList('git log', (err, list) => { + gitToList(t, 'git log', (err, list) => { t.error(err, 'no error') - t.ok(list?.length > 1, 'got a list') + t.ok(list?.length > 0, 'got a list') + if (!list || !list.length) { + return + } t.deepEqual( list[list.length - 9], @@ -119,7 +133,7 @@ test('current plain commit log', (t) => { t.deepEqual( list[list.length - 16], { - sha: list[list.length - 16].sha, // unknown at time of writing :) + sha: list[list.length - 16]?.sha, // unknown at time of writing :) authorDate: 'Tue Jun 12 23:41:35 2018 +0200', author: { name: 'Anna Henningsen', email: 'anna@addaleax.net' }, authors: [ @@ -130,19 +144,20 @@ test('current plain commit log', (t) => { }, 'got correct co-authored-by commit' ) - - t.end() }) }) test('current commit log with changes', (t) => { - gitToList('git log --stat', (err, list) => { + gitToList(t, 'git log --stat', (err, list) => { t.error(err, 'no errors') t.ok(list?.length > 0, 'got a list') + if (!list || !list.length) { + return + } t.deepEqual( - list[list.length - 4].changes, + list[list.length - 4]?.changes, { files: 1, insertions: 0, @@ -152,7 +167,7 @@ test('current commit log with changes', (t) => { ) t.deepEqual( - list[list.length - 3].changes, + list[list.length - 3]?.changes, { files: 1, insertions: 1, @@ -162,7 +177,7 @@ test('current commit log with changes', (t) => { ) t.deepEqual( - list[list.length - 2].changes, + list[list.length - 2]?.changes, { files: 1, insertions: 49, @@ -172,7 +187,7 @@ test('current commit log with changes', (t) => { ) t.deepEqual( - list[list.length - 1].changes, + list[list.length - 1]?.changes, { files: 1, insertions: 28, @@ -180,7 +195,5 @@ test('current commit log with changes', (t) => { }, 'got correct first commit changes' ) - - t.end() }) }) From b3c25ea683a0d580d893d471c19d453d7e97e6b9 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 4 Apr 2023 13:37:25 +1000 Subject: [PATCH 5/5] fix: full-depth checkout to make tests work --- .github/workflows/test-and-release.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test-and-release.yml b/.github/workflows/test-and-release.yml index c379ea8..a1b6d40 100644 --- a/.github/workflows/test-and-release.yml +++ b/.github/workflows/test-and-release.yml @@ -11,6 +11,8 @@ jobs: steps: - name: Checkout Repository uses: actions/checkout@v3 + with: + fetch-depth: 0 - name: Use Node.js ${{ matrix.node }} uses: actions/setup-node@v3.5.1 with: