Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grep-tests-from-pull-requests

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grep-tests-from-pull-requests - npm Package Compare versions

Comparing version 1.9.1 to 1.10.0

bin/should-pr-run-cypress-tests.js

3

bin/get-pr-body.js

@@ -62,4 +62,5 @@ #!/usr/bin/env node

console.log(body)
const tags = ['@log', '@sanity']
const prComments = []
const testsToRun = getTestsToRun(['@log', '@sanity'], body, prComments)
const testsToRun = getTestsToRun(body, tags, prComments)
console.log('tests to run')

@@ -66,0 +67,0 @@ console.log(testsToRun)

@@ -5,7 +5,3 @@ #!/usr/bin/env node

const arg = require('arg')
const {
getPullRequestNumber,
getPullRequestComments,
getTestsToRun,
} = require('../src/utils')
const { getPullRequestNumber, getPullRequestComments } = require('../src/utils')

@@ -12,0 +8,0 @@ const args = arg({

@@ -63,3 +63,4 @@ #!/usr/bin/env node

const prComments = await getPullRequestComments(options, envOptions)
const testsToRun = getTestsToRun(['@log', '@sanity'], body, prComments)
const tags = ['@log', '@sanity']
const testsToRun = getTestsToRun(body, tags, prComments)
console.log('tests to run')

@@ -66,0 +67,0 @@ console.log(testsToRun)

{
"name": "grep-tests-from-pull-requests",
"version": "1.9.1",
"version": "1.10.0",
"description": "Grabs the test tags to run from the pull request text",

@@ -17,3 +17,4 @@ "main": "src/index.js",

"get-pr-comments": "./bin/get-pr-comments.js",
"get-pr-tests": "./bin/get-pr-tests.js"
"get-pr-tests": "./bin/get-pr-tests.js",
"should-pr-run-cypress-tests": "./bin/should-pr-run-cypress-tests.js"
},

@@ -34,3 +35,3 @@ "repository": {

"devDependencies": {
"cypress": "^9.3.1",
"cypress": "^9.6.0",
"cypress-grep": "^2.13.1",

@@ -37,0 +38,0 @@ "prettier": "^2.5.1",

@@ -130,2 +130,10 @@ # grep-tests-from-pull-requests

## Skip / enable Cypress tests
You can find a checkbox in the pull request text to skip / run Cypress tests. This makes it simple to skip the E2E testing steps temporarily. Include the following checkbox line in the pull request body.
```
- [x] run Cypress tests
```
## Aliases

@@ -159,2 +167,12 @@

### should-pr-run-cypress-tests
Tells if the pull request body has a checkbox to run or skip the Cypress tests. If the tests should run, this script exits with code 0. If the PR disables the Cypress tests, it exits with code 1.
```
$ npx should-pr-run-cypress-tests --owner bahmutov --repo todomvc-no-tests-vercel --pull 12
$ echo $?
# 0 - we need to run the Cypress tests
```
## Debugging

@@ -161,0 +179,0 @@

@@ -65,3 +65,3 @@ /// <reference types="cypress" />

const prComments = await getPullRequestComments(prOptions, envOptions)
const testsToRun = getTestsToRun(options.tags, prBody, prComments)
const testsToRun = getTestsToRun(prBody, options.tags, prComments)
console.log('tests to run', testsToRun)

@@ -68,0 +68,0 @@ if (testsToRun) {

@@ -0,1 +1,5 @@

// @ts-check
const debug = require('debug')('grep-tests-from-pull-requests')
/**

@@ -53,2 +57,99 @@ * Finds and returns the test (base URL) in the given text line, if present.

module.exports = { getBaseUrlFromTextLine, getCypressEnvVariable, cast }
function shouldRunCypressTests(line) {
line = line.toLowerCase()
if (line.includes('[x] run cypress tests')) {
return true
}
if (line.includes('[ ] run cypress tests')) {
return false
}
if (line.includes('[x] run e2e tests')) {
return true
}
if (line.includes('[ ] run e2e tests')) {
return false
}
// otherwise return undefined - we do not know
// if the user wants to run Cypress tests
}
function findTestsToRun(pullRequestBody, tagsToLookFor = [], comments = []) {
const testsToRun = {
all: false,
tags: [],
baseUrl: null,
// additional environment variables to set found in the text
env: {},
}
const lines = pullRequestBody.split('\n')
lines.forEach((line) => {
const runCypressTests = shouldRunCypressTests(line)
if (typeof runCypressTests === 'boolean') {
testsToRun.runCypressTests = runCypressTests
}
const foundUrl = getBaseUrlFromTextLine(line)
if (foundUrl) {
debug('found base url: %s', foundUrl)
testsToRun.baseUrl = foundUrl
} else {
const envVariable = getCypressEnvVariable(line)
if (envVariable && 'key' in envVariable && 'value' in envVariable) {
debug('found env variable: %s', envVariable)
testsToRun.env[envVariable.key] = envVariable.value
}
}
})
// pull requests can overwrite the base url
comments.forEach((comment) => {
const commentLines = comment.split('\n')
commentLines.forEach((line) => {
const foundUrl = getBaseUrlFromTextLine(line)
if (foundUrl) {
debug('found base url in the comment: %s', foundUrl)
testsToRun.baseUrl = foundUrl
}
})
})
// find the test tags to run
if (!tagsToLookFor || !tagsToLookFor.length) {
debug('no tags to look for, running all tests')
testsToRun.all = true
return testsToRun
}
debug('looking for checkboxes with tags: %o', tagsToLookFor)
lines.forEach((line) => {
if (line.includes('all tests') && isLineChecked(line)) {
testsToRun.all = true
}
tagsToLookFor.forEach((tag) => {
if (line.includes(tag) && isLineChecked(line)) {
testsToRun.tags.push(tag)
}
})
})
return testsToRun
}
function isLineChecked(line) {
return line.includes('[x]')
}
module.exports = {
getBaseUrlFromTextLine,
getCypressEnvVariable,
cast,
shouldRunCypressTests,
findTestsToRun,
}
// @ts-check
const { getBaseUrlFromTextLine, getCypressEnvVariable } = require('./universal')
const { findTestsToRun } = require('./universal')
const got = require('got')

@@ -109,64 +109,10 @@ const debug = require('debug')('grep-tests-from-pull-requests')

function isLineChecked(line) {
return line.includes('[x]')
}
/**
* @param {string} pullRequestBody The pull request text with checkboxes
* @param {string[]} tagsToLookFor String tags to find in the pull request body
* @param {string} pullRequestBody The pull request text with checkboxes
* @param {PullRequestComment[]} pullRequestComments The pull request comments
*/
function getTestsToRun(tagsToLookFor, pullRequestBody, pullRequestComments) {
const testsToRun = {
all: false,
tags: [],
baseUrl: null,
// additional environment variables to set found in the text
env: {},
}
if (!tagsToLookFor || !tagsToLookFor.length) {
debug('no tags to look for, running all tests')
testsToRun.all = true
return testsToRun
}
debug('looking for checkboxes with tags: %o', tagsToLookFor)
const lines = pullRequestBody.split('\n')
lines.forEach((line) => {
if (line.includes('all tests') && isLineChecked(line)) {
testsToRun.all = true
}
const foundUrl = getBaseUrlFromTextLine(line)
if (foundUrl) {
debug('found base url: %s', foundUrl)
testsToRun.baseUrl = foundUrl
} else {
const envVariable = getCypressEnvVariable(line)
if (envVariable && 'key' in envVariable && 'value' in envVariable) {
debug('found env variable: %s', envVariable)
testsToRun.env[envVariable.key] = envVariable.value
}
}
tagsToLookFor.forEach((tag) => {
if (line.includes(tag) && isLineChecked(line)) {
testsToRun.tags.push(tag)
}
})
})
// pull requests can overwrite the base url
pullRequestComments.forEach((comment) => {
const commentLines = comment.body.split('\n')
commentLines.forEach((line) => {
const foundUrl = getBaseUrlFromTextLine(line)
if (foundUrl) {
debug('found base url in the comment: %s', foundUrl)
testsToRun.baseUrl = foundUrl
}
})
})
function getTestsToRun(pullRequestBody, tagsToLookFor, pullRequestComments) {
const comments = pullRequestComments.map((comment) => comment.body)
const testsToRun = findTestsToRun(pullRequestBody, tagsToLookFor, comments)
return testsToRun

@@ -173,0 +119,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc