Skip to content

Commit

Permalink
Add workflow for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
howard authored Jun 15, 2021
1 parent 3757b5c commit c97f398
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 12 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: CI
on:
push:
branches:
- '*'
tags-ignore:
- '*'
release:
types:
- created

jobs:
test_no_changes:
runs-on: ubuntu-latest
name: Test (no formatting changes)
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Run action
id: test-action
uses: findologic/intellij-format-action@main
with:
include-glob: '*.kt'
path: test/no_formatting_necessary
fail-on-changes: false

- name: Assert no files changed
run: '[[ ${{ steps.test-action.outputs.files-changed }} == 0 ]]'

test_with_changes:
runs-on: ubuntu-latest
name: Test (with formatting changes)
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Run action
id: test-action
uses: findologic/intellij-format-action@main
with:
include-glob: '*.kt'
path: test/formatting_necessary
fail-on-changes: false

- name: Assert file change detected
run: '[[ ${{ steps.test-action.outputs.files-changed }} -gt 0 ]]'

test_without_changes_different_pattern:
runs-on: ubuntu-latest
name: Test (no changes due to pattern mismatch)
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Run action
id: test-action
uses: findologic/intellij-format-action@main
with:
include-glob: '*.java'
path: test/formatting_necessary
fail-on-changes: false

- name: Assert no files changed
run: '[[ ${{ steps.test-action.outputs.files-changed }} == 0 ]]'

test_fail_on_changes:
runs-on: ubuntu-latest
name: Test (fail on changes enabled)
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Run action
id: test-action
uses: findologic/intellij-format-action@main
with:
include-glob: '*.kt'
path: test/formatting_necessary
fail-on-changes: true
continue-on-error: true

- name: Assert file change detected
run: '[[ ${{ steps.test-action.outputs.files-changed }} -gt 0 ]]'

- name: Assert action failed
run: '[[ ${{ steps.test-action.outcome }} == "failure" ]]'

- name: Assert conclusion is success, because continue-on-error
run: '[[ ${{ steps.test-action.conclusion }} == "success" ]]'
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
FROM openjdk:16-alpine
FROM ubuntu:latest
MAINTAINER FINDOLOGIC Developers <[email protected]>

RUN wget -O /tmp/idea.tar.gz https://download.jetbrains.com/idea/ideaIC-2021.1.2.tar.gz \
RUN apt-get update \
&& apt-get install -y bash git wget \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& wget --no-verbose -O /tmp/idea.tar.gz https://download.jetbrains.com/idea/ideaIC-2021.1.2.tar.gz \
&& cd /opt \
&& tar xzf /tmp/idea.tar.gz \
&& mv /opt/idea* /opt/idea \
&& rm /tmp/idea.tar.gz \
&& apk add --no-cache bash git
&& rm /tmp/idea.tar.gz
COPY entrypoint.sh /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Default: `false`.

### `files-changed`

Number of files changed by the formatter.
Zero if none changed, greater if at least one file changed.

## Example usage

Expand Down
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ inputs:
default: 'false'
outputs:
files-changed:
description: Number of files changed by the formatter.
description: Zero if none changed, greater if at least one file changed.
runs:
using: docker
image: Dockerfile
args:
- ${{ inputs.path }}
- ${{ inputs.include-glob }}
- ${{ inputs.path }}
- ${{ inputs.fail-on-changes }}
9 changes: 4 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash
set -e

# Wrapper for the formatter that passes action args and processes the output.
# Required args:
Expand All @@ -8,29 +7,29 @@ set -e
# - Whether to fail on file changes.

if [[ $# -ne 3 ]]; then
echo 'Exactly two parameters (base dir path, input file pattern, fail on changes) required.'
echo 'Exactly three parameters (base dir path, input file pattern, fail on changes) required.'
exit 1
fi

base_path=$1
include_pattern=$2
fail_on_changes=$3

cd "/github/workspace/$base_path"
cd "/github/workspace/$base_path" || exit 2
changed_files_before=$(git status --short)

/opt/idea/bin/format.sh -m $include_pattern -r .

changed_files_after=$(git status --short)
changed_files=$(diff <(echo "$changed_files_before") <(echo "$changed_files_after"))
changed_files_count=$(echo "$changed_files" | wc -l)
changed_files_count=$(($(echo "$changed_files" | wc --lines) - 1))

echo "$changed_files"
echo "::set-output name=files-changed::$changed_files_count"

if [[ "$fail_on_changes" == 'true' ]]; then
if [[ $changed_files_count -gt 0 ]]; then
echo 'Failing, because these files changed:'
echo "$changed_files"
exit 1
fi
fi
3 changes: 3 additions & 0 deletions test/formatting_necessary/Foobar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main(args: Array<String>) {
println("this has not been indented")
}
3 changes: 3 additions & 0 deletions test/no_formatting_necessary/Foobar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main(args: Array<String>) {
println("this is indented properly")
}

0 comments on commit c97f398

Please sign in to comment.