Skip to content

Commit

Permalink
feat: added util funciton to fetch storybook version and switch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshah98 committed Oct 25, 2023
1 parent c252c87 commit 9c585a4
Show file tree
Hide file tree
Showing 12 changed files with 9,322 additions and 10,824 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/test-storybook-v6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Test
on:
push:
workflow_dispatch:
inputs:
branch:
required: false
type: string
default: master
jobs:
test:
name: Test Storybook v6
strategy:
matrix:
os: [ubuntu-latest]
node: [14]
runs-on: ${{ matrix.os }}
steps:
- uses: actions-ecosystem/action-regex-match@v2

Check warning

Code scanning / Semgrep

Semgrep Finding: yaml.github-actions.security.third-party-action-not-pinned-to-commit-sha.third-party-action-not-pinned-to-commit-sha Warning test

An action sourced from a third-party repository on GitHub is not pinned to a full length commit SHA. Pinning an action to a full length commit SHA is currently the only way to use an action as an immutable release. Pinning to a particular SHA helps mitigate the risk of a bad actor adding a backdoor to the action's repository, as they would need to generate a SHA-1 collision for a valid Git object payload.
id: regex-match
if: ${{ github.event_name == 'workflow_dispatch' }}
with:
text: ${{ github.event.inputs.branch }}
regex: '^[a-zA-Z0-9_/\-]+$'
- name: Break on invalid branch name
run: exit 1
if: ${{ github.event_name == 'workflow_dispatch' && steps.regex-match.outputs && steps.regex-match.outputs.match == '' }}
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- run: ./prepare-storybook-v6-tests.sh
- uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}/node-${{ matrix.node }}/${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.os }}/node-${{ matrix.node }}/
- run: yarn
env:
PERCY_POSTINSTALL_BROWSER: true
- run: yarn build
- name: Set up @percy/cli from git
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
cd /tmp
git clone --branch ${{ github.event.inputs.branch }} --depth 1 https://github.com/percy/cli
cd cli
PERCY_PACKAGES=`find packages -type d -mindepth 1 -maxdepth 1 | sed -e 's/packages/@percy/g' | tr '\n' ' '`
echo "Packages: $PERCY_PACKAGES"
git log -1
yarn
yarn build
yarn global:link
cd ${{ github.workspace }}
yarn link `echo $PERCY_PACKAGES`
npx percy --version

Check failure

Code scanning / Semgrep

Semgrep Finding: yaml.github-actions.security.run-shell-injection.run-shell-injection Error test

Using variable interpolation ${...} with github context data in a run: step could allow an attacker to inject their own code into the runner. This would allow them to steal secrets and code. github context data can have arbitrary user input and should be treated as untrusted. Instead, use an intermediate environment variable with env: to store the data and use the environment variable in the run: script. Be sure to use double-quotes the environment variable, like this: "$ENVVAR".
- run: yarn test:coverage
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
default: master
jobs:
test:
name: Test
name: Test Storybook v7
strategy:
matrix:
os: [ubuntu-latest]
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"clean": "git clean -Xdf -e !node_modules -e !node_modules/**",
"lint": "eslint --ignore-path .gitignore .",
"readme": "percy-cli-readme",
"pretest": "node ./test/pretest.js",
"pretest": "storybook build --config-dir=./test/.storybook --output-dir=./test/.storybook-build --loglevel error",
"test": "yarn test:env jasmine --config=./test/jasmine.json",
"test:env": "cross-env NODE_ENV=test NODE_OPTIONS='--loader=./test/loader.js'",
"test:coverage": "nyc yarn test"
Expand All @@ -45,7 +45,7 @@
"@babel/eslint-parser": "^7.19.1",
"@babel/eslint-plugin": "^7.19.1",
"@babel/preset-env": "^7.22.9",
"@storybook/react": "^6.5.13",
"@storybook/react": "^7.5.0",
"@storybook/react-webpack5": "^7.5.0",
"babel-eslint": "^10.0.3",
"babel-plugin-istanbul": "^6.1.1",
Expand Down
66 changes: 66 additions & 0 deletions packageV6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@percy/storybook",
"version": "4.3.7",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/percy/percy-storybook.git"
},
"engine": {
"node": ">=16"
},
"files": [
"bin",
"dist"
],
"bin": {
"percy-storybook": "./bin/percy-storybook.cjs"
},
"main": "./dist/index.js",
"exports": "./dist/index.js",
"type": "module",
"scripts": {
"build": "babel src --out-dir dist",
"clean": "git clean -Xdf -e !node_modules -e !node_modules/**",
"lint": "eslint --ignore-path .gitignore .",
"readme": "percy-cli-readme",
"pretest": "build-storybook --config-dir=./test/.storybook --output-dir=./test/.storybook-build --loglevel error",
"test": "yarn test:env jasmine --config=./test/jasmine.json",
"test:env": "cross-env NODE_ENV=test NODE_OPTIONS='--loader=./test/loader.js'",
"test:coverage": "nyc yarn test"
},
"@percy/cli": {
"commands": [
"./dist/storybook.js"
]
},
"dependencies": {
"@percy/cli-command": "^1.24.0",
"cross-spawn": "^7.0.3",
"qs": "^6.11.0"
},
"devDependencies": {
"@babel/cli": "^7.23.0",
"@babel/core": "^7.23.2",
"@babel/eslint-parser": "^7.19.1",
"@babel/eslint-plugin": "^7.19.1",
"@babel/preset-env": "^7.22.9",
"@storybook/react": "^6.5.13",
"babel-eslint": "^10.0.3",
"babel-plugin-istanbul": "^6.1.1",
"cross-env": "^7.0.3",
"eslint": "^8.51.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-n": "^15.4.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"jasmine": "^4.5.0",
"jasmine-spec-reporter": "^7.0.0",
"mock-require": "^3.0.3",
"nock": "^13.2.9",
"nyc": "^15.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
5 changes: 5 additions & 0 deletions prepare-storybook-v6-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# move package and storybook configs
mv ./packageV6.json ./package.json
mv ./test/.storybook/mainV6.js ./test/.storybook/main.js
7 changes: 4 additions & 3 deletions src/start.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import command from '@percy/cli-command';
import * as common from './common.js';
import { checkStorybookVersion } from './utils.js';

export const start = command('start', {
description: 'Run start-storybook to snapshot stories',
Expand Down Expand Up @@ -31,12 +32,12 @@ export const start = command('start', {
let { takeStorybookSnapshots } = yield import('./snapshots.js');
let { default: { spawn } } = yield import('cross-spawn');
let { host, port } = flags;
let storybookVersion = process.env.STORYBOOK_VERSION || '6';
let storybookVersion = await checkStorybookVersion();

let args = storybookVersion === '7' ? ['dev'] : [];
let args = storybookVersion === 7 ? ['dev'] : [];
args = args.concat(['--ci', `--host=${host}`, `--port=${port}`, ...argv]);

let storybookBinary = storybookVersion === '7' ? 'storybook' : 'start-storybook';
let storybookBinary = storybookVersion === 7 ? 'storybook' : 'start-storybook';

log.info(`Running "${storybookBinary} ${args.join(' ')}"`);

Expand Down
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { request, createRootResource, yieldTo } from '@percy/cli-command/utils';
import spawn from 'cross-spawn';

// check storybook version
export function checkStorybookVersion() {
return new Promise((resolve, reject) => {
spawn('storybook', ['--version'])
.on('exit', (code) => { if (code === 0) resolve(7); })
.on('error', (err) => {
if (err.code === 'ENOENT') {
resolve(6);
}
reject(err);
});
});
}

// Transforms authorization credentials into a basic auth header and returns all config request
// headers with the additional authorization header if not already set.
Expand Down
14 changes: 4 additions & 10 deletions test/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
let storybookVersion = process.env.STORYBOOK_VERSION || '6';
let config = {
module.exports = {
stories: ['*.stories.js'],
features: {
postcss: false
}
};

if(storybookVersion === '7'){
config['framework'] = {
},
framework: {
name: '@storybook/react-webpack5',
options: {}
}
}

module.exports = config
};
6 changes: 6 additions & 0 deletions test/.storybook/mainV6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
stories: ['*.stories.js'],
features: {
postcss: false
}
};
8 changes: 0 additions & 8 deletions test/pretest.js

This file was deleted.

7 changes: 4 additions & 3 deletions test/storybook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import spawn from 'cross-spawn';
import { request } from '@percy/cli-command/utils';
import { api, logger, setupTest, createTestServer } from '@percy/cli-command/test/helpers';
import { storybook } from '../src/index.js';
import { checkStorybookVersion } from '../src/utils.js';

describe('percy storybook', () => {
let server, proc;
Expand All @@ -16,14 +17,14 @@ describe('percy storybook', () => {
default: () => [200, 'text/html', '<p>Not Storybook</p>']
});

let storybookVersion = process.env.STORYBOOK_VERSION || '6';
let args = storybookVersion === '7' ? ['dev'] : [];
let storybookVersion = await checkStorybookVersion();
let args = storybookVersion === 7 ? ['dev'] : [];
args = args.concat([
'--config-dir=./test/.storybook',
'--port=9000',
'--ci'
]);
let storybookBinary = storybookVersion === '7' ? 'storybook' : 'start-storybook';
let storybookBinary = storybookVersion === 7 ? 'storybook' : 'start-storybook';

proc = spawn(storybookBinary, args, { stdio: 'inherit' });

Expand Down
Loading

0 comments on commit 9c585a4

Please sign in to comment.