Merge pull request #8 from FranzDiebold/test/add-tests

Test/add tests
This commit is contained in:
Franz Diebold 2020-07-14 12:13:20 +02:00 committed by GitHub
commit 842d8e591f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 5393 additions and 84 deletions

3
.editorconfig Normal file
View file

@ -0,0 +1,3 @@
[*.js]
indent_style = space
indent_size = 2

15
.eslintrc.json Normal file
View file

@ -0,0 +1,15 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2020": true
},
"extends": [
"google"
],
"parserOptions": {
"ecmaVersion": 11
},
"rules": {
}
}

20
.github/workflows/ci.yml vendored Normal file
View file

@ -0,0 +1,20 @@
name: Lint and Test
on: [push]
jobs:
lint_and_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-node@v1
with:
node-version: '12'
- name: Install dependencies
run: make install
- name: Lint
run: make lint
- name: Test
run: make test

View file

@ -7,7 +7,7 @@ jobs:
name: Linux Demo name: Linux Demo
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: franzdiebold/github-env-vars-action@v1.1.1 - uses: franzdiebold/github-env-vars-action@v1.1.2
- name: Print environment variables - name: Print environment variables
run: | run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER" echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
@ -19,7 +19,7 @@ jobs:
name: Windows Demo name: Windows Demo
runs-on: windows-latest runs-on: windows-latest
steps: steps:
- uses: franzdiebold/github-env-vars-action@v1.1.1 - uses: franzdiebold/github-env-vars-action@v1.1.2
- name: Print environment variables - name: Print environment variables
run: | run: |
echo "GITHUB_REPOSITORY_OWNER=$Env:GITHUB_REPOSITORY_OWNER" echo "GITHUB_REPOSITORY_OWNER=$Env:GITHUB_REPOSITORY_OWNER"
@ -31,7 +31,7 @@ jobs:
name: macOS Demo name: macOS Demo
runs-on: macos-latest runs-on: macos-latest
steps: steps:
- uses: franzdiebold/github-env-vars-action@v1.1.1 - uses: franzdiebold/github-env-vars-action@v1.1.2
- name: Print environment variables - name: Print environment variables
run: | run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER" echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"

15
Makefile Normal file
View file

@ -0,0 +1,15 @@
.PHONY: install
install:
npm install
.PHONY: lint
lint:
npm run lint
.PHONY: test
test:
npm run test
.PHONY: build
build:
npm run build

View file

@ -1,8 +1,9 @@
# :octocat: :rocket: GitHub Environment Variables Action # :octocat: :rocket: GitHub Environment Variables Action
[![GitHub Action: View on Marketplace](https://img.shields.io/badge/GitHub%20Action-View_on_Marketplace-28a745?logo=github)](https://github.com/marketplace/actions/github-environment-variables-action) [![GitHub Action: View on Marketplace](https://img.shields.io/badge/GitHub%20Action-View_on_Marketplace-28a745?logo=github)](https://github.com/marketplace/actions/github-environment-variables-action)
[![Demo: available](https://img.shields.io/badge/Demo-available-orange)](https://github.com/FranzDiebold/github-env-vars-action/actions?query=workflow%3ADemo) [![Demo: available](https://img.shields.io/badge/Demo-available-orange)](.github/workflows/demo.yml)
[version](https://img.shields.io/github/package-json/v/FranzDiebold/github-env-vars-action/primary?label=version) [version](https://img.shields.io/github/package-json/v/FranzDiebold/github-env-vars-action/primary?label=version)
[![Lint and Test](https://github.com/FranzDiebold/github-env-vars-action/workflows/Lint%20and%20Test/badge.svg)](https://github.com/FranzDiebold/github-env-vars-action/actions?query=workflow%3A%22Lint+and+Test%22)
[![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE) [![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE)
A [GitHub Action](https://github.com/features/actions) to expose useful environment variables. A [GitHub Action](https://github.com/features/actions) to expose useful environment variables.
@ -32,7 +33,7 @@ For a full list of default environment variables exposed by GitHub see [https://
```yaml ```yaml
steps: steps:
- uses: franzdiebold/github-env-vars-action@v1.1.1 - uses: franzdiebold/github-env-vars-action@v1.1.2
- name: Print environment variables - name: Print environment variables
run: | run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER" echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"

112
dist/index.js vendored
View file

@ -157,67 +157,103 @@ module.exports = require("path");
/***/ }), /***/ }),
/***/ 622: /***/ 622:
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) { /***/ (function(module, __unusedexports, __webpack_require__) {
// Franz Diebold // Franz Diebold
const core = __webpack_require__(827); const core = __webpack_require__(827);
function getShaShort(fullSha) { /**
return fullSha ? fullSha.substring(0, 8) : null; * Get the repository owner from the repository string.
} * @param {string} repository
* @return {string} The owner of the repository.
*/
function getRepositoryOwner(repository) { function getRepositoryOwner(repository) {
return repository ? repository.split('/')[0] : null; return repository ? repository.split('/')[0] : null;
} }
/**
* Get the repository name from the repository string.
* @param {string} repository
* @return {string} The name of the repository.
*/
function getRepositoryName(repository) { function getRepositoryName(repository) {
return repository ? repository.split('/')[1] : null; return repository ? repository.split('/')[1] : null;
} }
/**
* Get the ref name from the ref string.
* @param {string} ref
* @return {string} The ref name.
*/
function getRefName(ref) { function getRefName(ref) {
return ref ? ref.split('/').slice(2).join('/') : null; return ref ? ref.split('/').slice(2).join('/') : null;
}
/**
* Get the short SHA from the full SHA.
* @param {string} fullSha
* @return {string} The short SHA.
*/
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
} }
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
try { try {
repository = process.env.GITHUB_REPOSITORY; // FranzDiebold/github-env-vars-action // i.e. FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY;
repositoryOwner = getRepositoryOwner(repository); repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) { if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner); core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`); core.info(`Set GITHUB_REPOSITORY_OWNER=` +
} else { `${process.env.GITHUB_REPOSITORY_OWNER}`);
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_OWNER".'); } else {
} core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_OWNER".');
}
repositoryName = getRepositoryName(repository); repositoryName = getRepositoryName(repository);
if (repositoryName) { if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName); core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`); core.info(`Set GITHUB_REPOSITORY_NAME=` +
} else { `${process.env.GITHUB_REPOSITORY_NAME}`);
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_NAME".'); } else {
} core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_NAME".');
}
refName = getRefName(process.env.GITHUB_REF); // refs/heads/feat/feature-branch-1 // i.e. refs/heads/feat/feature-branch-1
if (refName) { refName = getRefName(process.env.GITHUB_REF);
core.exportVariable('GITHUB_REF_NAME', refName); if (refName) {
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`); core.exportVariable('GITHUB_REF_NAME', refName);
} else { core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
core.warning('Environment variable "GITHUB_REF" not set. Cannot set "GITHUB_REF_NAME".'); } else {
} core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_NAME".');
}
shaShort = getShaShort(process.env.GITHUB_SHA); // ffac537e6cbbf934b08745a378932722df287a53 // i.e. ffac537e6cbbf934b08745a378932722df287a53
if (shaShort) { shaShort = getShaShort(process.env.GITHUB_SHA);
core.exportVariable('GITHUB_SHA_SHORT', shaShort); if (shaShort) {
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`); core.exportVariable('GITHUB_SHA_SHORT', shaShort);
} else { core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
core.warning('Environment variable "GITHUB_SHA" not set. Cannot set "GITHUB_SHA_SHORT".'); } else {
} core.warning('Environment variable "GITHUB_SHA" not set. ' +
'Cannot set "GITHUB_SHA_SHORT".');
}
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
} }
module.exports = {
getRepositoryOwner,
getRepositoryName,
getRefName,
getShaShort,
};
/***/ }), /***/ }),

110
index.js
View file

@ -2,57 +2,93 @@
const core = require('@actions/core'); const core = require('@actions/core');
function getShaShort(fullSha) { /**
return fullSha ? fullSha.substring(0, 8) : null; * Get the repository owner from the repository string.
} * @param {string} repository
* @return {string} The owner of the repository.
*/
function getRepositoryOwner(repository) { function getRepositoryOwner(repository) {
return repository ? repository.split('/')[0] : null; return repository ? repository.split('/')[0] : null;
} }
/**
* Get the repository name from the repository string.
* @param {string} repository
* @return {string} The name of the repository.
*/
function getRepositoryName(repository) { function getRepositoryName(repository) {
return repository ? repository.split('/')[1] : null; return repository ? repository.split('/')[1] : null;
} }
/**
* Get the ref name from the ref string.
* @param {string} ref
* @return {string} The ref name.
*/
function getRefName(ref) { function getRefName(ref) {
return ref ? ref.split('/').slice(2).join('/') : null; return ref ? ref.split('/').slice(2).join('/') : null;
}
/**
* Get the short SHA from the full SHA.
* @param {string} fullSha
* @return {string} The short SHA.
*/
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
} }
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
try { try {
repository = process.env.GITHUB_REPOSITORY; // FranzDiebold/github-env-vars-action // i.e. FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY;
repositoryOwner = getRepositoryOwner(repository); repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) { if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner); core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`); core.info(`Set GITHUB_REPOSITORY_OWNER=` +
} else { `${process.env.GITHUB_REPOSITORY_OWNER}`);
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_OWNER".'); } else {
} core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_OWNER".');
}
repositoryName = getRepositoryName(repository); repositoryName = getRepositoryName(repository);
if (repositoryName) { if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName); core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`); core.info(`Set GITHUB_REPOSITORY_NAME=` +
} else { `${process.env.GITHUB_REPOSITORY_NAME}`);
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_NAME".'); } else {
} core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_NAME".');
}
refName = getRefName(process.env.GITHUB_REF); // refs/heads/feat/feature-branch-1 // i.e. refs/heads/feat/feature-branch-1
if (refName) { refName = getRefName(process.env.GITHUB_REF);
core.exportVariable('GITHUB_REF_NAME', refName); if (refName) {
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`); core.exportVariable('GITHUB_REF_NAME', refName);
} else { core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
core.warning('Environment variable "GITHUB_REF" not set. Cannot set "GITHUB_REF_NAME".'); } else {
} core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_NAME".');
}
shaShort = getShaShort(process.env.GITHUB_SHA); // ffac537e6cbbf934b08745a378932722df287a53 // i.e. ffac537e6cbbf934b08745a378932722df287a53
if (shaShort) { shaShort = getShaShort(process.env.GITHUB_SHA);
core.exportVariable('GITHUB_SHA_SHORT', shaShort); if (shaShort) {
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`); core.exportVariable('GITHUB_SHA_SHORT', shaShort);
} else { core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
core.warning('Environment variable "GITHUB_SHA" not set. Cannot set "GITHUB_SHA_SHORT".'); } else {
} core.warning('Environment variable "GITHUB_SHA" not set. ' +
'Cannot set "GITHUB_SHA_SHORT".');
}
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
} }
module.exports = {
getRepositoryOwner,
getRepositoryName,
getRefName,
getShaShort,
};

44
index.test.js Normal file
View file

@ -0,0 +1,44 @@
const {
getRepositoryOwner, getRepositoryName, getRefName, getShaShort,
} = require('./index');
test('gets repository owner', () => {
expect(getRepositoryOwner('FranzDiebold/github-env-vars-action'))
.toEqual('FranzDiebold');
});
test('gets repository owner for empty GITHUB_REPOSITORY', () => {
expect(getRepositoryOwner(undefined)).toBeFalsy();
});
test('gets repository name', () => {
expect(getRepositoryName('FranzDiebold/github-env-vars-action'))
.toEqual('github-env-vars-action');
});
test('gets repository name for empty GITHUB_REPOSITORY', () => {
expect(getRepositoryName(undefined)).toBeFalsy();
});
test('gets ref from simple ref name', () => {
expect(getRefName('refs/heads/feature-branch-1'))
.toEqual('feature-branch-1');
});
test('gets ref from complex ref name', () => {
expect(getRefName('refs/heads/feat/feature-branch-1'))
.toEqual('feat/feature-branch-1');
});
test('gets repository name for empty GITHUB_REF_NAME', () => {
expect(getRefName(undefined)).toBeFalsy();
});
test('gets short SHA', () => {
expect(getShaShort('ffac537e6cbbf934b08745a378932722df287a53'))
.toEqual('ffac537e');
});
test('gets short SHA for empty GITHUB_SHA', () => {
expect(getShaShort(undefined)).toBeFalsy();
});

5135
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,12 @@
{ {
"name": "github-env-vars-action", "name": "github-env-vars-action",
"version": "1.1.1", "version": "1.1.2",
"description": "A GitHub Action to expose useful environment variables.", "description": "A GitHub Action to expose useful environment variables.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"build": "ncc build index.js", "lint": "eslint *.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "jest",
"build": "ncc build index.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -23,5 +24,10 @@
"homepage": "https://github.com/FranzDiebold/github-env-vars-action#readme", "homepage": "https://github.com/FranzDiebold/github-env-vars-action#readme",
"dependencies": { "dependencies": {
"@actions/core": "^1.2.4" "@actions/core": "^1.2.4"
},
"devDependencies": {
"eslint": "^7.4.0",
"eslint-config-google": "^0.14.0",
"jest": "^26.1.0"
} }
} }