Github desktop cannot commit with precommit hook - pre-commit-hook

I am using win10.
when clicking the blue button on the Github desktop to commit something.
error husky - pre-commit hook exited with code 1 (error) appears.
But if i type git commit -m "sth" in terminal, the pre-commit hook works fine
=============================
part of my package.json
"lint-staged": {
"*.{js,jsx,tsx,ts}": "eslint --cache --fix",
"*.{js,css,md,jsx,tsx,ts}": "prettier --write"
}
============================
.husky/pre-commit.sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
============================
.husky/_/husky.sh
#!/bin/sh
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}
readonly hook_name="$(basename "$0")"
debug "starting $hook_name..."
if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi
if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi
export readonly husky_skip_init=1
sh -e "$0" "$#"
exitCode="$?"
if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi
exit $exitCode
fi

The issue (even though it's not a real issue! ) is because of the hooks created by Husky. Husky is an npm package that lets you define npm scripts that correlate to local Git events such as a commit or push. And this helps in enforcing collaborative standards in a project. The quick solution, if you are too busy, is to simply delete the hooks folder for git which defines the pre-commit hooks and hence can push after that. (This is just kind of a hack to avoid editing thousands of files at a time for lint errors. Follow the guidelines and resolve all the lint errors for better code quality and maintainability. ) But it's always better to understand how Husky and hooks work and properly follow the lint warnings.
Edit: You can also skip hooks when you provide the git command line argument —no-verify,
git push origin master --no-verify
, or use Sourcetree‘s Bypass commit hooks setting (in the menu to the top right of the commit message field)

Related

nodejs failed but bash doesn't stop even I have set -e

I run a script in nodejs that trigger by bash that run by Jenkins pipeline.
The problem is the pipeline doesn't stop when the error happens. it should not run "echo git add ."
I add set -e - but seems that not work in this case.
What I can do to stop the execute in bash because error in the npm/node?
In Jenkins:
stage('Deploy') {
sh './scripts/deploy.sh'
}
In deploy.sh:
#!/bin/bash
set -e
npm run prepare-version
echo "git add ."
In package.json:
"scripts": { "prepare-version": "node ./scripts/prepare.js" }
prepare.js:
(async() => {
throw new Error('bug!!!');
})();
Your node.js file failed, though bash script executed it succesfully. Read and compare status code:
#!/bin/bash
set -e
npm run prepare-version
[[ $? != 0 ]] && exit;
echo 'git add .'

Use travis for build and testing with electron

I have an electron project with spectron tests. I use travis to build for every major OS with electron-builder. Now, I want to also run tests in travis, so I updated my .travis.yml file to run tests on linux (for headless tests) and build on osx. This works, but only the build part on osx works, while the tests fails.
This is my last try of travis config
language: node_js
matrix:
include:
- os: linux
- os: osx
osx_image: xcode10.2
node_js: '12'
addons:
chrome: stable
apt:
packages:
- xvfb
script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then xvfb-run test; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then npm run dist; fi
This fails without any valuable information
I have also tried this travis config
addons:
apt:
packages:
- xvfb
before_script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export DISPLAY=:99.0; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh -e /etc/init.d/xvfb start; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sleep 3; fi
script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then npm test; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then npm run dist; fi
that one fails because of timeout after 10 minutes. So I have no clue here of what it is going on. How can I tests my electron app in travis while also keeping the build on osx? Is there anything wrong on my travis config?
PD: Here is the repo where I have my project
Is it possible that you want to call
xvfb-run npm test
instead of
xvfb-run test
?
Otherwise, xvfb-run test will call the test command (reference) which returns 1 because there's no expression given i.e. expression evaluates to false.

Cucumber tests fail but travis build still passes

I am using Travis for CI. For some reason, the builds pass even when some tests fail. See the full log here
https://travis-ci.org/msm1089/hobnob/jobs/534173396
The way I am running the tests is via a bash script, e2e.test.sh, that is run by yarn.
Searching for this specific issue has not turned up anything that helps. It is something to do with exit codes I believe. I think I need to somehow get the build to exit with non-zero, but as you can see at bottom of the log, yarn exits with 0.
e2e.test.sh
#!/usr/bin/env bash
RETRY_INTERVAL=${RETRY_INTERVAL:-0.2}
# Run our API server as a background process
if [[ "$OSTYPE" == "msys" ]]; then
if ! netstat -aon | grep "0.0.0.0:$SERVER_PORT" | grep "LISTENING"; then
pm2 start --no-autorestart --name test:serve "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" -- run test:serve
until netstat -aon | grep "0.0.0.0:$SERVER_PORT" | grep "LISTENING"; do
sleep $RETRY_INTERVAL
done
fi
else
if ! ss -lnt | grep -q :$SERVER_PORT; then
yarn run test:serve &
fi
until ss -lnt | grep -q :$SERVER_PORT; do
sleep $RETRY_INTERVAL
done
fi
npx cucumber-js spec/cucumber/features --require-module #babel/register --require spec/cucumber/steps
if [[ "$OSTYPE" == "msys" ]]; then
pm2 delete test:serve
fi
travis.yml
language: node_js
node_js:
- 'node'
- 'lts/*'
- '10'
- '10.15.3'
services:
- elasticsearch
before_install:
- curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.deb
- sudo dpkg -i --force-confnew elasticsearch-6.6.1.deb
- sudo service elasticsearch restart
before_script:
- sleep 10
env:
global:
- NODE_ENV=test
- SERVER_PROTOCOL=http
- SERVER_HOSTNAME=localhost
- SERVER_PORT=8888
- ELASTICSEARCH_PROTOCOL=http
- ELASTICSEARCH_HOSTNAME=localhost
- ELASTICSEARCH_PORT=9200
- ELASTICSEARCH_INDEX=test
package.json
...
scripts:{
"test": "yarn run test:unit && yarn run test:integration && yarn run test:e2e"
}
...
So, how can I ensure that the cucumber exit code is the one that is returned, so that the build fails as it should when the tests don't pass?
There are a few possible ways to solve this. Here are two of my favorite.
Option 1:
Add set -e at the top of your bash script, so that it exits on first error, preserving the exit code, and subsequently, failing Travis if its a non zero.
Option 2:
Capture whatever exit code you want, and exit with it wherever it makes sense.
run whatever command here
exitcode=$?
[[ $exitcode == 0 ]] || exit $exitcode
As a side note - it seems like your bash script has too many responsibilities. I would consider separating them if possible, and then you give travis a list of commands to run, and possibly one or two before_script commands.
Something along these lines:
# .travis.yml
before_script:
- ./start_server.sh
script:
- npx cucumber-js spec/cucumber/features ...

How to run npm or angular 2 ng serve command from Bash Script?

I am looking to create an interactive bash script for our development team (never did this before) as our project has various needs and due to future growth on the team I want this to be easy as pi.
Normally for my work on the team I would change directory to the angular folder and run ng serve which kicks off the frontend development server. How do I do that in a bash script?
So far I have this:
#!/bin/bash
echo -e "Hello, "$USER".\nWelcome to the BICE build script!\nVersion 0.1"
echo -e "Which build option do you wish to use?\n1. Frontend (default)\n2. Backend\n3. Database\n4. Production"
echo -n "Enter your choice [1-4] or press [ENTER]:"
read choice
if [choice == 1]; then
echo "Option 1 selected"
cd /angular
#call ng serve
Thanks!
/angular seems weird as a working directory but if angular is a subdirectory of the one holding the script a
cd ./angular
ng serve
cd ..
should work...
Maybe you could look at this question so you can call the script from any working directory.
Also note that you are missing a fi at the end of your if, there sould be spaces after opening and before closing bracket and choice should be referenced with a $ sign:
#!/bin/bash
echo -e "Hello, "$USER".\nWelcome to the BICE build script!\nVersion 0.1"
echo -e "Which build option do you wish to use?\n1. Frontend (default)\n2. Backend\n3. Database\n4. Production"
echo -n "Enter your choice [1-4] or press [ENTER]:"
read choice
if [ $choice == 1 ]; then
echo "Option 1 selected"
cd ./angular
ng serve
cd ..
fi

How to make jenkins move to the next stage if its "terminal" has been blocked?

I'm trying to run http calls for testing a live web api that's going to run in the jenkins machine.
This is the pipeline script that's been used.
stage 'build'
node {
git url: 'https://github.com/juliomarcos/sample-http-test-ci/'
sh "npm install"
sh "npm start"
}
stage 'test'
node {
sh "npm test"
}
But jenkins won't move to the test step. How can I run npm test after the web app has fully started?
One approach is to start the web app with an & at the end so it will run in the background. i.e.
npm start &
You can try to redirect the output of npm start to a text file like this:
npm start > output.txt &
Then in the next step, loop until the "started" message is available, something like:
tail -f output.txt | while read LOGLINE
do
[[ "${LOGLINE}" == *"listening on port"* ]] && pkill -P $$ tail
done
Code not tested :)

Resources