Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stylelint #10

Merged
merged 1 commit into from Jul 12, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -103,6 +103,7 @@ docker_build() {
phpcs \
rubocop \
shellcheck \
stylelint \
syntax
}

@@ -20,5 +20,8 @@ steps:
- label: phpcs
command: script/phpcs

- label: stylelint
command: script/stylelint

- label: custom
command: script/custom
@@ -1,6 +1,6 @@
ENV:=tmp/env

DOCKER_IMAGES:=syntax,rubocop,shellcheck,editorconfig,eslint,gometalinter,phpcs
DOCKER_IMAGES:=syntax,rubocop,shellcheck,editorconfig,eslint,gometalinter,phpcs,stylelint
DOCKER_IMAGE_FILES:=$(shell find {$(DOCKER_IMAGES)} -print)

$(ENV): docker-compose.yml $(DOCKER_IMAGE_FILES) test/fake_api/*
@@ -37,6 +37,12 @@ services:
TEAMCI_REPO_SLUG: ~
TEAMCI_COMMIT: ~

stylelint:
build: stylelint
environment:
TEAMCI_REPO_SLUG: ~
TEAMCI_COMMIT: ~

api:
build: test/fake_api
ports:
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

set -euo pipefail

buildkite-agent meta-data set "teamci.${BUILDKITE_LABEL}.title" 'stylelint - CSS checking'

docker-compose run \
--rm \
-v "${CODE_DIR}:/code" \
-v "${CONFIG_DIR}:/config" \
-w "/code" \
stylelint
@@ -0,0 +1,11 @@
FROM node:7-alpine

RUN apk add --update bash ruby ruby-dev build-base jq
RUN npm install -g stylelint stylelint-config-standard
RUN gem install json --no-ri --no-rdoc

ADD stylelint-tap tapify.rb /usr/local/bin/
RUN mkdir /defaults
COPY config.json /defaults

CMD [ "stylelint-tap" ]
@@ -0,0 +1,3 @@
{
"extends": "/usr/local/lib/node_modules/stylelint-config-standard"
}
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

set -euo pipefail

main() {
local output exit_code config_file

output="$(mktemp)"
config_file=/config/stylelint/config.json

local -a args=("--formatter" "json")

echo '~~~ Checking configuration'
if [ -f "${config_file}" ]; then
echo 'INFO: using custom config.json'
args+=("--config" "../${config_file}")
else
echo 'INFO: no custom config.json found; using standard config.'
args+=("--config" "../defaults/config.json")
fi

echo '~~~ Running stylelint'
set +e
stylelint "${args[@]}" "**/*.{css,scss,less,sss}" > "${output}"
exit_code=$?
set -e

if [ "$(jq -re '. | length' "${output}")" -eq 0 ]; then
return 7
fi

tapify.rb < "${output}"

if [ "${exit_code}" -eq 0 ]; then
return 0
else
return 1
fi
}

main "$@"
@@ -0,0 +1,58 @@
#!/usr/bin/env ruby

$stdout.sync = true
$stderr.sync = true

require 'json'
require 'yaml'

def blob_url(file_name)
format('https://github.com/%<slug>s/blob/%<commit>s/%<file>s', {
slug: ENV.fetch('TEAMCI_REPO_SLUG'),
commit: ENV.fetch('TEAMCI_COMMIT'),
file: file_name
})
end

def warning_level(level)
case level.downcase
when 'error' then 'failure'
else 'warning'
end
end

report = JSON.parse($stdin.read)

$stdout.puts('--- TAP')
$stdout.puts("1..#{report.size}")

report.each_with_index do |data, i|
file_name = data.fetch('source').gsub(%r{^/}, '')
entries = data.fetch('warnings') + data.fetch('parseErrors')

if entries.empty?
$stdout.puts("ok #{i + 1} - #{file_name}")
else
$stdout.puts("not ok #{i + 1} - #{file_name}")

annotations = entries.map do |entry|
{
'filename' => file_name,
'blob_href' => blob_url(file_name),
'start_line' => entry.fetch('line'),
'end_line' => entry.fetch('line'),
'warning_level' => warning_level(entry.fetch('severity')),
'message' => entry.fetch('text'),
'title' => "stylelint: #{entry.fetch('rule')}"
}
end

yaml = YAML.dump(annotations).lines.map do |line|
" #{line}" # prepend two spaces for tap correctness
end
yaml << ' ...'
$stdout.puts(yaml.join)
end
end

$stdout.puts('--- TAP')
@@ -0,0 +1,97 @@
setup() {
buildkite-agent meta-data set 'teamci.access_token_url' "${TEAMCI_API_URL}"
buildkite-agent meta-data set 'teamci.head_sha' 'HEAD'

# Required metadata, but scripts continue if these cannot be cloned
buildkite-agent meta-data set 'teamci.config.repo' 'stylelint/config'
buildkite-agent meta-data set 'teamci.config.branch' 'skip'

rm -rf "${TEAMCI_CODE_DIR}/"*
}

@test "stylelint: invalid repo fails" {
buildkite-agent meta-data set 'teamci.repo.slug' 'stylelint/code'
buildkite-agent meta-data set 'teamci.head_branch' 'fail'

run test/emulate-buildkite script/stylelint

[ $status -eq 1 ]
[ -n "${output}" ]

[ "$(echo "${output}" | grep -cF -- '--- TAP')" -eq 2 ]

# Test for annotation keys
echo "${output}" | grep -qF 'filename:'
echo "${output}" | grep -qF 'blob_href:'
echo "${output}" | grep -qF 'start_line:'
echo "${output}" | grep -qF 'end_line:'
echo "${output}" | grep -qF 'warning_level:'
echo "${output}" | grep -qF 'message:'
echo "${output}" | grep -qF 'title:'

[ -n "$(buildkite-agent meta-data get 'teamci.stylelint.title')" ]
}

@test "stylelint: parse errors" {
buildkite-agent meta-data set 'teamci.repo.slug' 'stylelint/code'
buildkite-agent meta-data set 'teamci.head_branch' 'parse_error'

run test/emulate-buildkite script/stylelint

[ $status -eq 1 ]
[ -n "${output}" ]

[ "$(echo "${output}" | grep -cF -- '--- TAP')" -eq 2 ]

# Test for annotation keys
echo "${output}" | grep -qF 'filename:'
echo "${output}" | grep -qF 'blob_href:'
echo "${output}" | grep -qF 'start_line:'
echo "${output}" | grep -qF 'end_line:'
echo "${output}" | grep -qF 'warning_level:'
echo "${output}" | grep -qF 'message:'
echo "${output}" | grep -qF 'title:'

[ -n "$(buildkite-agent meta-data get 'teamci.stylelint.title')" ]
}

@test "stylelint: valid repo passes" {
buildkite-agent meta-data set 'teamci.repo.slug' 'stylelint/code'
buildkite-agent meta-data set 'teamci.head_branch' 'pass'

run test/emulate-buildkite script/stylelint

[ $status -eq 0 ]
[ -n "${output}" ]

[ "$(echo "${output}" | grep -cF -- '--- TAP')" -eq 2 ]

[ -n "$(buildkite-agent meta-data get 'teamci.stylelint.title')" ]
}

@test "stylelint: skips when no matching files" {
buildkite-agent meta-data set 'teamci.repo.slug' 'stylelint/code'
buildkite-agent meta-data set 'teamci.head_branch' 'skip'

run test/emulate-buildkite script/stylelint

[ $status -eq 7 ]
[ -n "${output}" ]

[ -n "$(buildkite-agent meta-data get 'teamci.stylelint.title')" ]
}

@test "stylelint: config file exists" {
buildkite-agent meta-data set 'teamci.repo.slug' 'stylelint/code'
buildkite-agent meta-data set 'teamci.head_branch' 'config_file'
buildkite-agent meta-data set 'teamci.config.repo' 'stylelint/config'
buildkite-agent meta-data set 'teamci.config.branch' 'config_file'

run test/emulate-buildkite script/stylelint

# The configured options should make the failing fixture pass
[ $status -eq 0 ]
[ -n "${output}" ]

[ -n "$(buildkite-agent meta-data get 'teamci.stylelint.title')" ]
}
@@ -0,0 +1,3 @@
.test {
font-family: bar;
}
@@ -0,0 +1,3 @@
someJunk {
foo: bar;
}
@@ -0,0 +1 @@
::::
@@ -0,0 +1,3 @@
.test {
color: black;
}
@@ -0,0 +1 @@
console.log('hello world');
@@ -0,0 +1 @@
puts "hello world"
No changes.
@@ -0,0 +1,6 @@
{
"extends": "/usr/local/lib/node_modules/stylelint-config-standard",
"rules": {
"font-family-no-missing-generic-family-keyword": null
}
}
No changes.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.