From f88fc2bacc941644531f98edb671f526251e1bd8 Mon Sep 17 00:00:00 2001 From: Ilya Khaprov Date: Sun, 30 Jul 2017 23:38:56 +0300 Subject: [PATCH] use credo in tests --- .credo.exs | 64 +++++++++++++++++++++++++++++++ .travis.yml | 2 +- bin/checks.sh | 3 ++ bin/increment-version | 89 +++++++++++++++++++++++++++++++++++++++++++ bin/pre-commit.sh | 18 +++++++++ 5 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 .credo.exs create mode 100755 bin/checks.sh create mode 100755 bin/increment-version create mode 100755 bin/pre-commit.sh diff --git a/.credo.exs b/.credo.exs new file mode 100644 index 0000000..37bca19 --- /dev/null +++ b/.credo.exs @@ -0,0 +1,64 @@ +%{ + configs: [ + %{ + name: "default", + check_for_updates: false, + files: %{ + included: ["lib/", "src/", "web/", "apps/", "test/"], + excluded: [~r"/_build/", ~r"/deps/"] + }, + checks: [ + {Credo.Check.Consistency.ExceptionNames}, + {Credo.Check.Consistency.LineEndings}, + {Credo.Check.Consistency.SpaceAroundOperators}, + {Credo.Check.Consistency.SpaceInParentheses}, + {Credo.Check.Consistency.TabsOrSpaces}, + + {Credo.Check.Design.AliasUsage, false}, + + {Credo.Check.Design.DuplicatedCode, excluded_macros: [:schema, :setup, :test]}, + + {Credo.Check.Design.TagTODO, exit_status: 0}, + {Credo.Check.Design.TagFIXME, exit_status: 0}, + + {Credo.Check.Readability.FunctionNames}, + {Credo.Check.Readability.LargeNumbers}, + {Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 90, exit_status: 0}, + {Credo.Check.Readability.ModuleAttributeNames}, + {Credo.Check.Readability.ModuleDoc, false}, + {Credo.Check.Readability.ModuleNames}, + {Credo.Check.Readability.ParenthesesInCondition}, + {Credo.Check.Readability.PredicateFunctionNames}, + {Credo.Check.Readability.TrailingBlankLine}, + {Credo.Check.Readability.TrailingWhiteSpace}, + {Credo.Check.Readability.VariableNames}, + + {Credo.Check.Refactor.ABCSize}, + {Credo.Check.Refactor.CondStatements}, + {Credo.Check.Refactor.FunctionArity}, + {Credo.Check.Refactor.MatchInCondition}, + {Credo.Check.Refactor.PipeChainStart}, + {Credo.Check.Refactor.CyclomaticComplexity}, + {Credo.Check.Refactor.NegatedConditionsInUnless}, + {Credo.Check.Refactor.NegatedConditionsWithElse}, + {Credo.Check.Refactor.Nesting}, + {Credo.Check.Refactor.UnlessWithElse}, + + {Credo.Check.Warning.IExPry}, + {Credo.Check.Warning.IoInspect}, + {Credo.Check.Warning.NameRedeclarationByAssignment}, + {Credo.Check.Warning.NameRedeclarationByCase}, + {Credo.Check.Warning.NameRedeclarationByDef}, + {Credo.Check.Warning.NameRedeclarationByFn}, + {Credo.Check.Warning.OperationOnSameValues}, + {Credo.Check.Warning.BoolOperationOnSameValues}, + {Credo.Check.Warning.UnusedEnumOperation}, + {Credo.Check.Warning.UnusedKeywordOperation}, + {Credo.Check.Warning.UnusedListOperation}, + {Credo.Check.Warning.UnusedStringOperation}, + {Credo.Check.Warning.UnusedTupleOperation}, + {Credo.Check.Warning.OperationWithConstantResult}, + ] + } + ] +} diff --git a/.travis.yml b/.travis.yml index e7b9328..e3aa44b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,4 +9,4 @@ sudo: false before_script: - mix deps.get --only test script: - - mix test + - ./bin/checks.sh diff --git a/bin/checks.sh b/bin/checks.sh new file mode 100755 index 0000000..b5476ee --- /dev/null +++ b/bin/checks.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +MIX_ENV=test mix credo --strict && MIX_ENV=test mix test diff --git a/bin/increment-version b/bin/increment-version new file mode 100755 index 0000000..d854625 --- /dev/null +++ b/bin/increment-version @@ -0,0 +1,89 @@ +#!/bin/bash + +# Based on https://github.com/fmahnke/shell-semver +# The MIT License (MIT) +# Copyright (c) 2014 Fritz Mahnke +# Increment a version string using Semantic Versioning (SemVer) terminology. +# Parse command line options. + + +while getopts ":Mmp" Option +do + case $Option in + M ) major=true;; + m ) minor=true;; + p ) patch=true;; + esac +done + +shift $(($OPTIND - 1)) + +STAGED_COUNT=$(git diff --cached --numstat | wc -l) +UNSTAGED_COUNT=$(git diff --numstat | wc -l) + +## TODO check we are on master +if [ $STAGED_COUNT -ne "0" ]; then + echo "you have staged changes. Aborting". + exit 1 +fi + +if [ $UNSTAGED_COUNT -ne "0" ]; then + echo "you have unstaged changes. Aborting". + exit 1 +fi + +version="$(mix run --no-compile --no-start -e ":io.format('~s', [PrometheusPhoenix.Mixfile.project()[:version]])")" + +echo "Old version: ${version}" + +# Build array from version string. +oa=( ${version//./ } ) +a=( ${version//./ } ) + +# If version string is missing or has the wrong number of members, show usage message. + +if [ ${#a[@]} -ne 3 ] +then + echo "usage: $(basename $0) [-Mmp] TAG_MESSAGE(optional)" + exit 1 +fi + +# Increment version numbers as requested. + +if [ ! -z $major ] +then + ((a[0]++)) + a[1]=0 + a[2]=0 +fi + +if [ ! -z $minor ] +then + ((a[1]++)) + a[2]=0 +fi + +if [ ! -z $patch ] +then + ((a[2]++)) +fi + +new_version="${a[0]}.${a[1]}.${a[2]}" + +echo "New version: ${new_version}" + +sed -i s/\"${oa[0]}\.${oa[1]}\.${oa[2]}\"/\"${a[0]}\.${a[1]}\.${a[2]}\"/g mix.exs +sed -i s/\~\>\ ${oa[0]}\.${oa[1]}/\~\>\ ${a[0]}\.${a[1]}/g README.md + +git add mix.exs +git add README.md +git add doc +git commit -m "Bump to v${new_version}" + +TAG_MESSAGE=${1:-"New version: v${new_version}"} + +git tag -a "v${new_version}" -m "${TAG_MESSAGE}" +git push origin master +git push origin "v${new_version}" + +mix hex.publish diff --git a/bin/pre-commit.sh b/bin/pre-commit.sh new file mode 100755 index 0000000..f7f90ff --- /dev/null +++ b/bin/pre-commit.sh @@ -0,0 +1,18 @@ +#!/bin/sh +if [ $# -eq 0 ]; then + git stash -q --keep-index + ./bin/checks.sh + RESULT=$? + git stash pop -q + [ $RESULT -ne 0 ] && exit 1 + exit 0 +else + if [ $1 = "install" ]; then + ln -s `pwd`/bin/pre-commit.sh .git/hooks/pre-commit + elif [ $1 = "uninstall" ]; then + rm .git/hooks/pre-commit + else + echo "Unknown argument $1" + exit 1 + fi +fi