name: 'Test the action'
on:
  pull_request:
    branches:
      - master
      - releases/*

jobs:
  test-json:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v1.5.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file.json'

      - name: Show file
        run: |
          echo "$(<test-file.json)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file.json'
          EXPECTED_VERSION: '1.5.0'

  test-json-new:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.1.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file-new.json'

      - name: Show file
        run: |
          echo "$(<test-file-new.json)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file-new.json'
          EXPECTED_VERSION: '0.1.0'

  test-json-empty:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - run: touch ./test-file-empty.json

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.1.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: './test-file-empty.json'

      - run: echo "$(<./test-file-empty.json)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file-empty.json'
          EXPECTED_VERSION: '0.1.0'

  test-git:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"

      - run: git tag | xargs git tag -d
      - name: Create fake tag
        run: "git tag -a 'v0.55.8' -m 'v0.55.8'"
      - run: "git add . && git commit --allow-empty -m 'fix: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.55.9'
          SKIPPED_COMMIT: true
        with:
          github-token: ${{ secrets.github_token }}
          skip-commit: 'true'
          
  test-git-no-pull:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"

      - run: git tag | xargs git tag -d
      - name: Create fake tag
        run: "git tag -a 'v0.55.8' -m 'v0.55.8'"
      - run: "git add . && git commit --allow-empty -m 'fix: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.55.9'
          SKIPPED_COMMIT: true
          SKIPPED_PULL: true
        with:
          github-token: ${{ secrets.github_token }}
          skip-commit: 'true'
          skip-git-pull: 'true'

  test-git-fallback:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - run: git tag | xargs git tag -d

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.1.0'
          SKIPPED_COMMIT: true
        with:
          github-token: ${{ secrets.github_token }}
          skip-commit: 'true'

  test-git-no-push:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"

      - run: git tag | xargs git tag -d
      - name: Create fake tag
        run: "git tag -a 'v0.55.8' -m 'v0.55.8'"
      - run: "git add . && git commit --allow-empty -m 'fix: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.55.9'
          SKIPPED_COMMIT: true
          EXPECTED_NO_PUSH: true
        with:
          github-token: ${{ secrets.github_token }}
          skip-commit: 'true'
          git-push: 'false'

  test-yaml:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v9.5.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file.yaml'
          version-path: 'package.version'

      - name: Generate changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v9.6.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file.yaml'
          version-path: 'package.no-quotes-version'

      - name: Generate changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v9.7.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file.yaml'
          version-path: 'package.double-quotes-version'

      - name: Show file
        run: |
          echo "$(<test-file.yaml)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file.yaml'
          EXPECTED_VERSION: '9.5.0'
          EXPECTED_VERSION_PATH: 'package.version'

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file.yaml'
          EXPECTED_VERSION: '9.6.0'
          EXPECTED_VERSION_PATH: 'package.no-quotes-version'

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file.yaml'
          EXPECTED_VERSION: '9.7.0'
          EXPECTED_VERSION_PATH: 'package.double-quotes-version'

  test-yaml-new:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - run: touch ./test-file-empty.yaml

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.1.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file-new.yaml'
          version-path: 'package.version'

      - name: Show file
        run: |
          echo "$(<test-file-new.yaml)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file-new.yaml'
          EXPECTED_VERSION: '0.1.0'
          EXPECTED_VERSION_PATH: 'package.version'

  test-yaml-empty:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.1.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: './test-file-empty.yaml'
          version-path: 'package.version'

      - name: Show file
        run: |
          echo "$(<test-file-empty.yaml)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file-empty.yaml'
          EXPECTED_VERSION: '0.1.0'
          EXPECTED_VERSION_PATH: 'package.version'

  test-toml:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.10.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file.toml'
          version-path: 'package.version'

      - name: Show file
        run: |
          echo "$(<test-file.toml)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file.toml'
          EXPECTED_VERSION: '0.10.0'
          EXPECTED_VERSION_PATH: 'package.version'

  test-toml-new:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.1.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file-new.toml'
          version-path: 'package.version'

      - name: Show file
        run: |
          echo "$(<test-file-new.toml)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file-new.toml'
          EXPECTED_VERSION: '0.1.0'
          EXPECTED_VERSION_PATH: 'package.version'

  test-toml-empty:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - run: touch ./test-file-empty.toml

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v6.5.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: './test-file-empty.toml'
          version-path: 'package.version'
          fallback-version: '6.5.0'

      - name: Show file
        run: |
          echo "$(<test-file-empty.toml)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file-empty.toml'
          EXPECTED_VERSION: '6.5.0'
          EXPECTED_VERSION_PATH: 'package.version'

  test-pre-commit:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - run: test -f pre-commit.test.json && (echo should not be here yet && exit 1) || exit 0

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v1.5.0'
        with:
          github-token: ${{ secrets.github_token }}
          pre-commit: './test-pre-commit.js'
          skip-on-empty: 'false'
          version-file: './test-file.json'

      - run: test -f pre-commit.test.json || (echo should be here && exit 1)
      - run: cat pre-commit.test.json && echo ""
      - run: cat ./package.json

  test-pre-changelog-generation:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - run: test -f pre-changelog-generation.test.json && (echo should not be here yet && exit 1) || exit 0

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v1.0.100-alpha'
        with:
          github-token: ${{ secrets.github_token }}
          pre-changelog-generation: './test-pre-changelog-generation.js'
          version-file: './test-file.toml'
          version-path: 'package.version'

      - run: test -f pre-changelog-generation.version.test.json || (echo should be here && exit 1)
      - run: test -f pre-changelog-generation.tag.test.json || (echo should be here && exit 1)
      - run: cat pre-changelog-generation.version.test.json && echo ""
      - run: cat pre-changelog-generation.tag.test.json && echo ""
      - run: cat ./test-file.toml

  test-multiple-files:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v1.5.0'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file.json, test-file-2.json, test-file.toml, test-file.yaml'

      - name: Show files
        run: |
          echo "Test json file"
          echo "$(<test-file.json)\n\n"
          echo "Test json 2 file"
          echo "$(<test-file-2.json)\n\n"
          echo "Test toml file"
          echo "$(<test-file.toml)\n\n"
          echo "Test yaml file"
          echo "$(<test-file.yaml)\n\n"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file.json, test-file-2.json, test-file.toml, test-file.yaml'
          EXPECTED_VERSION: '1.5.0, 1.5.0, 1.5.0, 1.11.0'

  test-config-file-path:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate Changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v0.1.0'
        with:
          github-token: ${{ secrets.github_token }}
          skip-version-file: 'true'
          config-file-path: './test-changelog.config.js'

      - name: Test output
        run: |
          if grep -q "### New Features" "./CHANGELOG.md"; then
            echo "Generated changelog file has config applied"
          else
            echo "Changelog config not applied" && exit 1
          fi


  test-skip-ci:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          path: "./"

      - run: npm ci --prod

      - run: "git config --global user.email 'changelog@github.com'"
      - run: "git config --global user.name 'Awesome Github action'"
      - run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"

      - name: Generate changelog
        id: changelog
        uses: ./
        env:
          ENV: 'dont-use-git'
          EXPECTED_TAG: 'v1.5.0'
          SKIP_CI: 'false'
        with:
          github-token: ${{ secrets.github_token }}
          version-file: 'test-file.json'
          skip-ci: 'false'

      - name: Show file
        run: |
          echo "$(<test-file.json)"

      - name: Test output
        run: node ./test-output.js
        env:
          FILES: 'test-file.json'
          EXPECTED_VERSION: '1.5.0'


