Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from ZupIT/unit-tests
Browse files Browse the repository at this point in the history
solve bugs, some improvements and unit tests
  • Loading branch information
thallesfreitaszup authored Jan 19, 2022
2 parents 4d3fba2 + 1bd1cdc commit 635176f
Show file tree
Hide file tree
Showing 5,847 changed files with 655,089 additions and 4,132 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
54 changes: 54 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# * Copyright 2021, 2022 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
#
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the master branch
pull_request:
branches: [ main ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Run actions checkout
uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- name: Run actions cache
uses: actions/cache@c64c572235d810460d0d6876e9c705ad5002b353
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Use Node.js 12.x
uses: actions/setup-node@f1f314fca9dfce2769ece7d933488f076716723e
with:
node-version: 12.x
- name: Run npm ci
run: npm ci
- name: Run tests
run: npm test
17 changes: 16 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
#
# * Copyright 2021, 2022 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
#
# This is a basic workflow to help you get started with Actions

name: CI
name: main

# Controls when the workflow will run
on:
Expand Down
25 changes: 21 additions & 4 deletions file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
* Copyright 2021, 2022 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,24 @@ const checkBOMInJSON = (stringFile) => {
}
return stringFile
}

const readAndValidateConfigFile = (fileName) => {
const fileData = readFile(fileName)
if (!fileData) {
const errorMsg = `Missing ${fileName}`
core.setFailed(errorMsg)
throw new Error()
}
const fileFiltered = checkBOMInJSON(fileData)
let dataObject = JSON.parse(fileFiltered)
if (!dataObject.copyright || !dataObject.startDateLicense) {
const errorMsg = "Missing copyright or startDateLicense entry on config file. Check your config.json."
core.setFailed(errorMsg)
throw new Error()
}
return dataObject
}
module.exports = {
readFile, checkBOMInJSON
}
readFile, checkBOMInJSON, readAndValidateConfigFile
}


33 changes: 21 additions & 12 deletions license.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
* Copyright 2021, 2022 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,16 +19,17 @@ const github = require('@actions/github')
const fs = require("fs");
const util = require("util");
const chalk = require("chalk");
function hasCorrectCopyrightDate(copyrightFile, startDateLicense) {

function getRequiredDateLicenseText(startDateLicense) {
const currentYear = new Date().getFullYear()
let requiredDate = ''
if (startDateLicense < currentYear) {
requiredDate = `Copyright ${startDateLicense}, ${currentYear}`
} else {
requiredDate = `Copyright ${startDateLicense}`
return `Copyright ${startDateLicense}, ${currentYear}`
}

return copyrightFile.includes(requiredDate)
return `Copyright ${startDateLicense}`
}
function hasCorrectCopyrightDate(copyrightFile, startDateLicense) {
const requiredDateText = getRequiredDateLicenseText(startDateLicense)
return copyrightFile.includes(requiredDateText)
}

async function openFile(name) {
Expand All @@ -44,6 +45,14 @@ async function openFile(name) {
})
}

function checkLineOnFile(line, copyrightFile, fileName) {
if (!copyrightFile.includes(line)) {
console.log('File '+chalk.yellow(fileName+": ") + chalk.red(`Missing or Misspelling the required text: "${line}" `))
return false
}
return true
}

async function checkLicenseFile(fileName, config, fd) {
let buffer = Buffer.alloc(8000)
return await new Promise(
Expand All @@ -54,11 +63,11 @@ async function checkLicenseFile(fileName, config, fd) {
}
const copyrightFile = buffer.toString('utf-8')
const allCopyrightIncluded = config.copyrightContent.every(
line => copyrightFile.includes(line)
line => checkLineOnFile(line, copyrightFile, fileName)
)

if (!allCopyrightIncluded) {
console.log('File '+chalk.yellow(fileName+": ") + chalk.red('No copyright header!'))
console.log('File '+chalk.yellow(fileName+": ") + chalk.red('Wrong license header!'))
reject(fileName)
} else {

Expand All @@ -67,7 +76,8 @@ async function checkLicenseFile(fileName, config, fd) {
console.log('File ' + chalk.yellow(fileName+": ") + chalk.green('ok!'))
resolve()
} else {
console.log('File '+ chalk.yellow(fileName+": ")+ chalk.red('Fix copyright date!'))
const requiredDateMessage = `Fix license header date! Expected "${getRequiredDateLicenseText(config.startDateLicense)}"`
console.log('File '+ chalk.yellow(fileName+": ")+ chalk.red(requiredDateMessage))
reject(fileName)
}
}
Expand Down Expand Up @@ -107,7 +117,6 @@ function removeIgnoredFiles(filesPr, fileNames) {

const checkLicense = async (fileNames, config) => {
const token = core.getInput('token') || process.env.TOKEN

const octokit = github.getOctokit(token)
const prNumber = github.context.payload.pull_request.number
const owner = github.context.payload.repository.owner.login
Expand Down
Loading

0 comments on commit 635176f

Please sign in to comment.