diff --git a/.circleci/README.md b/.circleci/README.md new file mode 100644 index 0000000..c3757f5 --- /dev/null +++ b/.circleci/README.md @@ -0,0 +1,33 @@ +# Orb Development Pipeline + +This configuration file uses [orb-tools orb]() version 10 to automatically _pack_, _test_, and _publish_ CircleCI orbs using this project structure. View the comments within the config file for a full break down + +## Overview: + +**Imported Orbs** + +Both orb-tools and a development version of your orb will be imported into the config. On the first run, a `dev:alpha` development tag _must_ exist on your orb, but will be handled automatically from there on. + +**Jobs** + +In the _jobs_ key, you will define _integration tests_. These jobs will utilize the functionality of your orb at run-time and attempt to validate their usage with live examples. Integration tests can be an excellent way of determining issues with parameters and run-time execution. + +### Workflows + +There are two workflows which automate the pack, test, and publishing process. + +**test-pack** + +This is the first of the two workflows run. This workflow is responsible for any testing or prepping prior to integration tests. This is where linting occurs, shellchecking, BATS tests, or anything else that can be be tested without the need for further credentials. + +This Workflow will be placed on _hold_ prior to publishing a new development version of the orb (based on this commit), as this step requires access to specific publishing credentials. + +This allows users to fork the orb repository and begin the pipeline, while the code-owners review that the code is safe to test in an environment where publishing keys will be present. + +Once approved, the development version of the orb will publish and the _trigger-integration-tests-workflow_ job will run, kicking off the next workflow + +**integration-test_deploy** + +The second and final workflow is manually triggered by the _trigger-integration-tests-workflow_ job. In this run, the development version of the orb that was just published will be imported, and the integration tests will run. + +When running on the `master` branch (after merging to `master`), the workflow will additionally publish your new production orb. \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..163fd61 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,101 @@ +version: 2.1 + +orbs: + # Replace this with your own! + rollbar-orb: adlibertas/rollbar-orb@<> + orb-tools: circleci/orb-tools@10.0 + bats: circleci/bats@1.0 + shellcheck: circleci/shellcheck@2.0 + +# Pipeline Parameters +## These parameters are used internally by orb-tools. Skip to the Jobs section. +parameters: + run-integration-tests: + description: An internal flag to prevent integration test from running before a development version has been created. + type: boolean + default: false + dev-orb-version: + description: > + The development version of the orb to test. + This value is automatically adjusted by the "trigger-integration-tests-workflow" job to correspond with the specific version created by the commit and should not be edited. + A "dev:alpha" version must exist for the initial pipeline run. + type: string + default: "dev:alpha" + +jobs: + # Define one or more jobs which will utilize your orb's commands and parameters to validate your changes. + integration-test-1: + docker: + - image: cimg/base:stable + steps: + - checkout + # "greet" is a sample command packaged with this orb config. + # This sample integration test will run as long as the greet command exists. Once you remove the greet command you should remove this line. + # Push new changes first, before adding new tests to your config. + - rollbar-orb/greet + +workflows: + # Prior to producing a development orb (which requires credentials) basic validation, linting, and even unit testing can be performed. + # This workflow will run on every commit + test-pack: + unless: << pipeline.parameters.run-integration-tests >> + jobs: + - orb-tools/lint # Lint Yaml files + - orb-tools/pack # Pack orb source + - shellcheck/check: + dir: ./src/scripts + exclude: SC2148 + # optional: Run BATS tests against your scripts + - bats/run: + path: ./src/tests + # If you accept building open source forks, protect your secrects behind a restricted context. + # A job containing restricted context (which holds your orb publishing credentials) may only be accessed by a user with proper permissions. + # An open source user may begin a pipeline with a PR, and once the pipeline is approved by an authorized user at this point, the pipeline will continue with the proper context permissions. + - hold-for-dev-publish: + type: approval + requires: + - orb-tools/lint + - orb-tools/pack + - bats/run + - shellcheck/check + # Publish development version(s) of the orb. + - orb-tools/publish-dev: + orb-name: adlibertas/rollbar-orb + context: orb-publishing # A restricted context containing your private publishing credentials. Will only execute if approved by an authorized user. + requires: [hold-for-dev-publish] + # Trigger an integration workflow to test the + # dev:${CIRCLE_SHA1:0:7} version of your orb + - orb-tools/trigger-integration-tests-workflow: + name: trigger-integration-dev + context: orb-publishing + requires: + - orb-tools/publish-dev + + # This `integration-test_deploy` workflow will only run + # when the run-integration-tests pipeline parameter is set to true. + # It is meant to be triggered by the "trigger-integration-tests-workflow" + # job, and run tests on @dev:${CIRCLE_SHA1:0:7}. + integration-test_deploy: + when: << pipeline.parameters.run-integration-tests >> + jobs: + # Run any integration tests defined within the `jobs` key. + - integration-test-1 + # Publish a semver version of the orb. relies on + # the commit subject containing the text "[semver:patch|minor|major|skip]" + # as that will determine whether a patch, minor or major + # version will be published or if publishing should + # be skipped. + # e.g. [semver:patch] will cause a patch version to be published. + - orb-tools/dev-promote-prod-from-commit-subject: + orb-name: adlibertas/rollbar-orb + context: orb-publishing + add-pr-comment: false + fail-if-semver-not-indicated: true + publish-version-tag: false + requires: + - integration-test-1 + filters: + branches: + only: + - master + - main diff --git a/.github/ISSUE_TEMPLATE/BUG.md b/.github/ISSUE_TEMPLATE/BUG.md new file mode 100644 index 0000000..7f1ad45 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/BUG.md @@ -0,0 +1,33 @@ +--- +name: "\U0001F41E Bug report" +about: Report any bugs encountered while using this orb. +title: '' +labels: bug +assignees: '' + +--- + +## Orb version: + + + +## What happened: + + + +## Expected behavior: + + + +## Additional Information: + + \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md new file mode 100644 index 0000000..df221b1 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md @@ -0,0 +1,14 @@ +--- +name: "\U0001F680 Feature Request" +about: Propose changes to the orb. +title: '' +labels: feature_request +assignees: '' +--- + +## Describe Request: + +## Examples: + +## Supporting Documentation Links: + diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..3ba13e0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1 @@ +blank_issues_enabled: false diff --git a/.github/PULL_REQUEST_TEMPLATE/PULL_REQUEST.md b/.github/PULL_REQUEST_TEMPLATE/PULL_REQUEST.md new file mode 100644 index 0000000..c9ebfd7 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE/PULL_REQUEST.md @@ -0,0 +1,33 @@ + +**SEMVER Update Type:** +- [ ] Major +- [ ] Minor +- [ ] Patch + +## Description: + + + +## Motivation: + + + + **Closes Issues:** +- ISSUE URL + +## Checklist: + + + +- [ ] All new jobs, commands, executors, parameters have descriptions. +- [ ] Usage Example version numbers have been updated. +- [ ] Changelog has been updated. \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..19eafbb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# orb.yml is "packed" from source, and not published directly from the repository. +orb.yml \ No newline at end of file diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..c9a8a2e --- /dev/null +++ b/.yamllint @@ -0,0 +1,7 @@ +extends: relaxed + +rules: + line-length: + max: 200 + allow-non-breakable-inline-mappings: true + diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..8038dfb --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + - Current development changes [ to be moved to release ] + +## [1.0.0] - YYYY-MM-DD +### Added + - Initial Release +### Changed + - Initial Release +### Removed + - Initial Release + + +[1.0.0]: GITHUB TAG URL diff --git a/LICENSE b/LICENSE index c6ccda7..8d7cb6b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Rollbar +Copyright (c) 2021 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index e89c94a..8ce15ab 100644 --- a/README.md +++ b/README.md @@ -1,67 +1,41 @@ -# rollbar-orb -[CircleCI Orb](https://github.com/CircleCI-Public/config-preview-sdk/tree/master/docs) for reporting deploys and uploading sourcemaps to Rollbar. +# Orb Project Template -## Requirements -If you don't have them already, create accounts in [Rollbar](https://rollbar.com/signup) and [CircleCI](https://circleci.com/signup/). +[![CircleCI Build Status](https://circleci.com/gh/adeubank/rollbar-orb.svg?style=shield "CircleCI Build Status")](https://circleci.com/gh/adeubank/rollbar-orb) [![CircleCI Orb Version](https://img.shields.io/badge/endpoint.svg?url=https://badges.circleci.io/orb/adeubank/rollbar-orb)](https://circleci.com/orbs/registry/orb/adeubank/rollbar-orb) [![GitHub License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://raw.githubusercontent.com/adeubank/rollbar-orb/master/LICENSE) [![CircleCI Community](https://img.shields.io/badge/community-CircleCI%20Discuss-343434.svg)](https://discuss.circleci.com/c/ecosystem/orbs) -In order to use the Rollbar orb, your CircleCI builds need to run in environment where [curl](https://curl.haxx.se/) and [jq](https://stedolan.github.io/jq/) are installed. -## Usage -To use the Rollbar orb, reference it in your project and then use one of the included commands: -* `notify_deploy` -* `notify_deploy_started` -* `notify_deploy_finished` -* `upload_sourcemap` +A starter template for orb projects. Build, test, and publish orbs automatically on CircleCI with [Orb-Tools](https://circleci.com/orbs/registry/orb/circleci/orb-tools). -Documentation for each method is available inline in [orb.yml](https://github.com/rollbar/rollbar-orb/blob/master/src/rollbar/orb.yml). +Additional READMEs are available in each directory. -Here's a very simple example of a CircleCI config that uses the Rollbar orb to report a deploy starting and finishing as well as uploading sourcemaps: -```yaml -version: 2.1 -orbs: - rollbar: rollbar/deploy@volatile -jobs: - build: - docker: - - image: circleci/ruby:latest - environment: - ROLLBAR_ACCESS_TOKEN: {{ your `POST_SERVER_ITEM` access token here}} - ROLLBAR_ENVIRONMENT: development - steps: - - checkout - - rollbar/notify_deploy_started: - environment: $ROLLBAR_ENVIRONMENT - - rollbar/upload_sourcemap: - minified_url: https://rollbar.com/rollbar.min.js - source_map: static/js/rollbar.js.map - js_files: static/js/rollbar.js - - # The rest of your build steps go here... - # Notify Rollbar when your deploy has completed successfully - - rollbar/notify_deploy_finished: - deploy_id: $ROLLBAR_DEPLOY_ID - status: succeeded -``` +## Resources -## Help / Support +[CircleCI Orb Registry Page](https://circleci.com/orbs/registry/orb/adeubank/rollbar-orb) - The official registry page of this orb for all versions, executors, commands, and jobs described. +[CircleCI Orb Docs](https://circleci.com/docs/2.0/orb-intro/#section=configuration) - Docs for using and creating CircleCI Orbs. -If you run into any issues, please email us at [support@rollbar.com](mailto:support@rollbar.com) +### How to Contribute -For bug reports, please [open an issue on GitHub](https://github.com/rollbar/rollbar-orb/issues/new). +We welcome [issues](https://github.com/adeubank/rollbar-orb/issues) to and [pull requests](https://github.com/adeubank/rollbar-orb/pulls) against this repository! +### How to Publish +* Create and push a branch with your new features. +* When ready to publish a new production version, create a Pull Request from _feature branch_ to `master`. +* The title of the pull request must contain a special semver tag: `[semver:]` where `` is replaced by one of the following values. -## Contributing +| Increment | Description| +| ----------| -----------| +| major | Issue a 1.0.0 incremented release| +| minor | Issue a x.1.0 incremented release| +| patch | Issue a x.x.1 incremented release| +| skip | Do not issue a release| -For details on how to do development work on a CircleCI orb, check out [Circle CI's Orb docs](https://circleci.com/docs/2.0/creating-orbs/). +Example: `[semver:major]` -To contribute to the Rollbar orb: +* Squash and merge. Ensure the semver tag is preserved and entered as a part of the commit message. +* On merge, after manual approval, the orb will automatically be published to the Orb Registry. -1. [Fork it](https://github.com/rollbar/rollbar-orb) -2. Create your feature branch (```git checkout -b my-new-feature```). -3. Commit your changes (```git commit -am 'Added some feature'```) -4. Push to the branch (```git push origin my-new-feature```) -5. Create a new Pull Request and submit it for review. + +For further questions/comments about this or other orbs, visit the Orb Category of [CircleCI Discuss](https://discuss.circleci.com/c/orbs). diff --git a/src/@orb.yml b/src/@orb.yml new file mode 100755 index 0000000..2e8ae44 --- /dev/null +++ b/src/@orb.yml @@ -0,0 +1,15 @@ +version: 2.1 + +description: > + Sample orb description +# What will your orb allow users to accomplish? +# Descriptions should be short, simple, and informative. + +# This information will be displayed in the orb registry and is not mandatory. +display: + home_url: "https://www.website.com/docs" + source_url: "https://www.github.com/EXAMPLE_ORG/EXAMPLE_PROJECT" + +# If your orb requires other orbs, you can import them like this. Otherwise remove the "orbs" stanza. +# orbs: +# hello: circleci/hello-build@0.0.5 diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..5b96be4 --- /dev/null +++ b/src/README.md @@ -0,0 +1,26 @@ +# Orb Source + +Orbs are shipped as individual `orb.yml` files, however, to make development easier, it is possible to author an orb in _unpacked_ form, which can be _packed_ with the CircleCI CLI and published. + +The default `.circleci/config.yml` file contains the configuration code needed to automatically pack, test, and deploy and changes made to the contents of the orb source in this directory. + +## @orb.yml + +This is the entry point for our orb "tree", which becomes our `orb.yml` file later. + +Within the `@orb.yml` we generally specify 4 configuration keys + +**Keys** + +1. **version** + Specify version 2.1 for orb-compatible configuration `version: 2.1` +2. **description** + Give your orb a description. Shown within the CLI and orb registry +3. **display** + Specify the `home_url` referencing documentation or product URL, and `source_url` linking to the orb's source repository. +4. **orbs** + (optional) Some orbs may depend on other orbs. Import them here. + +## See: + - [Orb Author Intro](https://circleci.com/docs/2.0/orb-author-intro/#section=configuration) + - [Reusable Configuration](https://circleci.com/docs/2.0/reusing-config) \ No newline at end of file diff --git a/src/commands/README.md b/src/commands/README.md new file mode 100644 index 0000000..bd3b6b0 --- /dev/null +++ b/src/commands/README.md @@ -0,0 +1,27 @@ +# Commands + +Easily add and author [Reusable Commands](https://circleci.com/docs/2.0/reusing-config/#authoring-reusable-commands) to the `src/commands` directory. + +Each _YAML_ file within this directory will be treated as an orb command, with a name which matches its filename. + +View the included _[greet.yml](./greet.yml)_ example. + +```yaml +description: > + Replace this text with a description for this command. + # What will this command do? + # Descriptions should be short, simple, and clear. +parameters: + greeting: + type: string + default: "Hello" + description: "Select a proper greeting" +steps: + - run: + name: Hello World + command: echo << parameters.greeting >> world +``` + +## See: + - [Orb Author Intro](https://circleci.com/docs/2.0/orb-author-intro/#section=configuration) + - [How to author commands](https://circleci.com/docs/2.0/reusing-config/#authoring-reusable-commands) diff --git a/src/commands/greet.yml b/src/commands/greet.yml new file mode 100755 index 0000000..f4e2205 --- /dev/null +++ b/src/commands/greet.yml @@ -0,0 +1,15 @@ +description: > + This command echos "Hello World" using file inclusion. +# What will this command do? +# Descriptions should be short, simple, and clear. +parameters: + to: + type: string + default: "World" + description: "Hello to whom?" +steps: + - run: + environment: + PARAM_TO: <> + name: Hello Greeting + command: <> diff --git a/src/examples/README.md b/src/examples/README.md new file mode 100644 index 0000000..73d38b6 --- /dev/null +++ b/src/examples/README.md @@ -0,0 +1,13 @@ +# Usage Examples + +Easily author and add [Usage Examples](https://circleci.com/docs/2.0/orb-author/#providing-usage-examples-of-orbs) to the `src/examples` directory. + +Each _YAML_ file within this directory will be treated as an orb usage example, with a name which matches its filename. + +View the included _[example.yml](./example.yml)_ example. + +Usage examples should contain clear use-case based example configurations for using the orb. + + +## See: + - [Providing Usage examples](https://circleci.com/docs/2.0/orb-author/#providing-usage-examples-of-orbs) \ No newline at end of file diff --git a/src/examples/example.yml b/src/examples/example.yml new file mode 100755 index 0000000..a2cdaab --- /dev/null +++ b/src/examples/example.yml @@ -0,0 +1,13 @@ +description: > + Sample example description. +# Provide a use-case based example for using this orb. +# Everything in the `usage` section will be displayed in the orb registry. +# Comments are not retained. +usage: + version: 2.1 + orbs: + : /@1.2.3 + workflows: + use-my-orb: + jobs: + - / diff --git a/src/executors/README.md b/src/executors/README.md new file mode 100644 index 0000000..37c0efb --- /dev/null +++ b/src/executors/README.md @@ -0,0 +1,29 @@ +# Executors + +Easily author and add [Parameterized Executors](https://circleci.com/docs/2.0/reusing-config/#executors) to the `src/executors` directory. + +Each _YAML_ file within this directory will be treated as an orb executor, with a name which matches its filename. + +Executors can be used to parameterize the same environment across many jobs. Orbs nor jobs _require_ executors, but may be helpful in some cases, such as: [parameterizing the Node version for a testing job so that matrix testing may be used](https://circleci.com/orbs/registry/orb/circleci/node#usage-run_matrix_testing). + +View the included _[hello.yml](./hello.yml)_ example. + + +```yaml +description: > + This is a sample executor using Docker and Node. +docker: + - image: 'cimg/node:<>' +parameters: + tag: + default: lts + description: > + Pick a specific circleci/node image variant: + https://hub.docker.com/r/cimg/node/tags + type: string +``` + +## See: + - [Orb Author Intro](https://circleci.com/docs/2.0/orb-author-intro/#section=configuration) + - [How To Author Executors](https://circleci.com/docs/2.0/reusing-config/#authoring-reusable-executors) + - [Node Orb Executor](https://github.com/CircleCI-Public/node-orb/blob/master/src/executors/default.yml) \ No newline at end of file diff --git a/src/executors/default.yml b/src/executors/default.yml new file mode 100755 index 0000000..61e9948 --- /dev/null +++ b/src/executors/default.yml @@ -0,0 +1,11 @@ +description: > + This is a sample executor using Docker and Node. +docker: + - image: 'cimg/node:<>' +parameters: + tag: + default: lts + description: > + Pick a specific circleci/node image variant: + https://hub.docker.com/r/cimg/node/tags + type: string diff --git a/src/jobs/README.md b/src/jobs/README.md new file mode 100644 index 0000000..5dddab9 --- /dev/null +++ b/src/jobs/README.md @@ -0,0 +1,30 @@ +# Jobs + +Easily author and add [Parameterized Jobs](https://circleci.com/docs/2.0/reusing-config/#authoring-parameterized-jobs) to the `src/jobs` directory. + +Each _YAML_ file within this directory will be treated as an orb job, with a name which matches its filename. + +Jobs may invoke orb commands and other steps to fully automate tasks with minimal user configuration. + +View the included _[hello.yml](./hello.yml)_ example. + + +```yaml + # What will this job do? + # Descriptions should be short, simple, and clear. + Sample description +executor: default +parameters: + greeting: + type: string + default: "Hello" + description: "Select a proper greeting" +steps: + - greet: + greeting: << parameters.greeting >> +``` + +## See: + - [Orb Author Intro](https://circleci.com/docs/2.0/orb-author-intro/#section=configuration) + - [How To Author Commands](https://circleci.com/docs/2.0/reusing-config/#authoring-parameterized-jobs) + - [Node Orb "test" Job](https://github.com/CircleCI-Public/node-orb/blob/master/src/jobs/test.yml) \ No newline at end of file diff --git a/src/jobs/hello.yml b/src/jobs/hello.yml new file mode 100755 index 0000000..f7ec411 --- /dev/null +++ b/src/jobs/hello.yml @@ -0,0 +1,15 @@ +description: > + Sample description +# What will this job do? +# Descriptions should be short, simple, and clear. + +executor: default + +parameters: + to: + type: string + default: "World" + description: "Hello to whom?" +steps: + - greet: + to: << parameters.to >> diff --git a/src/rollbar/orb.yml b/src/rollbar/orb.yml index 1753fc3..bc47776 100644 --- a/src/rollbar/orb.yml +++ b/src/rollbar/orb.yml @@ -44,6 +44,10 @@ commands: status: type: enum enum: ["succeeded","failed","timed_out"] + when: + type: enum + enum: ["on_fail","on_success","always"] + default: always steps: - run: name: Rollbar - Notify Deploy Finished @@ -51,6 +55,7 @@ commands: curl -X PATCH \ https://api.rollbar.com/api/1/deploy/<< parameters.deploy_id>>?access_token=$ROLLBAR_ACCESS_TOKEN \ --data '{"status":"<< parameters.status >>"}' + when: << parameters.when >> notify_deploy: description: | diff --git a/src/scripts/README.md b/src/scripts/README.md new file mode 100644 index 0000000..5554e19 --- /dev/null +++ b/src/scripts/README.md @@ -0,0 +1,40 @@ +# scripts/ + +This is where any scripts you wish to include in your orb can be kept. This is encouraged to ensure your orb can have all aspects tested, and is easier to author, since we sacrifice most features an editor offers when editing scripts as text in YAML. + +As a part of keeping things seperate, it is encouraged to use environment variables to pass through parameters, rather than using the `<< parameter. >>` syntax that CircleCI offers. + +# Including Scripts + +Utilizing the `circleci orb pack` CLI command, it is possible to import files (such as _shell scripts_), using the `<>` syntax in place of any config key's value. + +```yaml +# commands/greet.yml +description: > + This command echos "Hello World" using file inclusion. +parameters: + to: + type: string + default: "World" + description: "Hello to who?" +steps: + - run: + environment: + PARAM_TO: < + name: Hello < + command: <> + +``` + +```shell +# scripts/greet.sh +Greet() { + echo Hello ${PARAM_TO} +} + +# Will not run if sourced from another script. This is done so this script may be tested. +# View src/tests for more information. +if [[ "$_" == "$0" ]]; then + Greet +fi +``` \ No newline at end of file diff --git a/src/scripts/greet.sh b/src/scripts/greet.sh new file mode 100644 index 0000000..e6febce --- /dev/null +++ b/src/scripts/greet.sh @@ -0,0 +1,10 @@ +Greet() { + echo Hello "${PARAM_TO}" +} + +# Will not run if sourced for bats-core tests. +# View src/tests for more information. +ORB_TEST_ENV="bats-core" +if [ "${0#*$ORB_TEST_ENV}" == "$0" ]; then + Greet +fi diff --git a/src/tests/README.md b/src/tests/README.md new file mode 100644 index 0000000..ccd0b07 --- /dev/null +++ b/src/tests/README.md @@ -0,0 +1,92 @@ +# tests/ + +This is where your testing scripts for whichever language is embeded in your orb live, which can be executed locally and within a CircleCI pipeline prior to publishing. + +# Testing Orbs + +This orb is built using the `circleci orb pack` command, which allows the _command_ logic to be separated out into separate _shell script_ `.sh` files. Because the logic now sits in a known and executable language, it is possible to perform true unit testing using existing frameworks such a [BATS-Core](https://github.com/bats-core/bats-core#installing-bats-from-source). + +## **Example _command.yml_** + +```yaml + +description: A sample command + +parameters: + source: + description: "source path parameter example" + type: string + default: src + +steps: + - run: + name: "Ensure destination path" + environment: + ORB_SOURCE_PATH: <> + command: <> +``` + +## **Example _command.sh_** + +```bash + +CreatePackage() { + cd "$ORB_SOURCE_PATH" && make + # Build some application at the source location + # In this example, let's assume given some + # sample application and known inputs, + # we expect a certain logfile would be generated. +} + +# Will not run if sourced from another script. +# This is done so this script may be tested. +if [[ "$_" == "$0" ]]; then + CreatePackage +fi + +``` + +We want our script to execute when running in our CI environment or locally, but we don't want to execute our script if we are testing it. In the case of testing, we only want to source the functions within our script,t his allows us to mock inputs and test individual functions. + +**A POSIX Compliant Source Checking Method:** + +```shell +# Will not run if sourced for bats. +# View src/tests for more information. +TEST_ENV="bats-core" +if [ "${0#*$TEST_ENV}" == "$0" ]; then + RUN CODE +fi +``` + +**Example _command_tests.bats_** + +BATS-Core is a useful testing framework for shell scripts. Using the "source checking" methods above, we can source our shell scripts from within our BATS tests without executing any code. This allows us to call specific functions and test their output. + +```bash +# Runs prior to every test. +setup() { + # Load functions from our script file. + # Ensure the script will not execute as + # shown in the above script example. + source ./src/scripts/command.sh +} + +@test '1: Test Build Results' { + # Mock environment variables or functions by exporting them (after the script has been sourced) + export ORB_SOURCE_PATH="src/my-sample-app" + CreatePackage + # test the results + grep -e 'RESULT="success"' log.txt +} + +``` + +Tests can contain any valid shell code. Any error codes returned during a test will result in a test failure. + +In this example, we grep the contents of `log.txt.` which should contain a `success` result if the `CreatePackage` function we had loaded executed successfully. + +## See: + - [BATS Orb](https://circleci.com/orbs/registry/orb/circleci/bats) + - [Orb Testing CircleCI Docs](https://circleci.com/docs/2.0/testing-orbs) + - [BATS-Core GitHub](https://github.com/bats-core/bats-core) diff --git a/src/tests/greet.bats b/src/tests/greet.bats new file mode 100644 index 0000000..a7bfbe0 --- /dev/null +++ b/src/tests/greet.bats @@ -0,0 +1,13 @@ +# Runs prior to every test +setup() { + # Load our script file. + source ./src/scripts/greet.sh +} + +@test '1: Greet the world' { + # Mock environment variables or functions by exporting them (after the script has been sourced) + export PARAM_TO="World" + # Capture the output of our "Greet" function + result=$(Greet) + [ "$result" == "Hello World" ] +} \ No newline at end of file