From 4f571db979c6e5acde4bd95b678649e1b17cca84 Mon Sep 17 00:00:00 2001 From: Thomas Cooper Date: Sat, 21 Oct 2023 12:47:35 -0400 Subject: [PATCH] only use include on PRs, otherwise ignore --- nyc.config.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/nyc.config.js b/nyc.config.js index dc22ec462..32a852f26 100644 --- a/nyc.config.js +++ b/nyc.config.js @@ -2,20 +2,32 @@ const { exec } = require('child_process'); -console.log('Generating coverage report for changed files...'); -const files = []; -exec('git diff --name-only HEAD~1', (err, stdout, stderr) => { - if (err) { - console.error(err + stderr); - return; - } - files.push(...stdout.split('\n')); -}); - -module.exports = { - include: files, +const opts = { branches: 80, lines: 80, functions: 80, statements: 80, }; + +// Only generate coverage report for changed files in PR +// see: https://github.com/actions/checkout/issues/438#issuecomment-1446882066 +// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables +if (process.env.GITHUB_BASE_REF !== undefined) { + console.log('Generating coverage report for changed files...'); + const baseRef = process.env.GITHUB_BASE_REF; + const headRef = process.env.GITHUB_SHA; + const files = []; + exec( + `git diff --name-only origin/${baseRef}..${headRef}`, + (err, stdout, stderr) => { + if (err) { + console.error(err + stderr); + return; + } + files.push(...stdout.split('\n')); + }, + ); + opts['include'] = files; +} + +module.exports = opts;