Skip to content

Unit testing support #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5d1ed4f
find test files, separate from cpp files
ianfixes Jan 18, 2018
64736b4
move cpp files in preparation for alternate test files dir
ianfixes Jan 18, 2018
bfc3362
unit tests working, rough cut
ianfixes Jan 19, 2018
740b57a
unit tests with TAP output
ianfixes Jan 22, 2018
12e3680
remove unused gcc / avr-gcc code
ianfixes Jan 22, 2018
f469faa
more rubocop rules muted
ianfixes Jan 22, 2018
b8a25d0
add overridable config
ianfixes Jan 22, 2018
55a9ef6
ignore all bin artifacts
ianfixes Jan 23, 2018
545f437
add config section for packages and urls
ianfixes Jan 23, 2018
ec47c44
better logic around library installation
ianfixes Jan 23, 2018
1706e92
remove build-with-gcc; we want test-with-gcc only
ianfixes Jan 23, 2018
77b1e11
rename methods for build / unittest platform lists
ianfixes Jan 23, 2018
dd05470
add ability to specify aux libraries
ianfixes Jan 23, 2018
df56e67
better prefs cache especially on read before write
ianfixes Jan 23, 2018
227f320
rename method to reflect boards not board being installed
ianfixes Jan 23, 2018
a7c420d
make sure paths are expanded when linking libraries
ianfixes Jan 23, 2018
66f3fff
return an array, don't yield to a block
ianfixes Jan 23, 2018
066c79a
allow overriding config, set standard naming convention
ianfixes Jan 23, 2018
e9eb4af
commenting
ianfixes Jan 23, 2018
9409f08
factor out gcc args
ianfixes Jan 23, 2018
f8ce3ba
try new CI script
ianfixes Jan 23, 2018
bac46d7
handle case where board package is already installed, and return success
ianfixes Jan 24, 2018
d696dbd
hide chattiness of system commands behind member variables
ianfixes Jan 24, 2018
b1ea18b
print errors on failure
ianfixes Jan 24, 2018
bb4b9dd
stop testing on failing platform arduino:samd:zero
ianfixes Jan 24, 2018
d7f95b0
more handling of stdout
ianfixes Jan 24, 2018
13c8564
fix cpp_library reference in script
ianfixes Jan 24, 2018
8be5e24
Track command and output of GCC
ianfixes Jan 24, 2018
8c1aabe
conditionally print logs -- not on final failure
ianfixes Jan 24, 2018
9dc9d4e
don't suppress code files that begin with the word test
ianfixes Jan 24, 2018
32f80c4
separate example project for CI from example project for rspec
ianfixes Jan 24, 2018
e0135ff
pragma once
ianfixes Jan 24, 2018
bd97bcf
fix library.properties
ianfixes Jan 24, 2018
62934bf
fixes for test runner script
ianfixes Jan 24, 2018
04fa574
scale back default architectures
ianfixes Jan 24, 2018
a574146
avoid problems related to finding a symbolic link
ianfixes Jan 24, 2018
d828085
tweaks to CI
ianfixes Jan 24, 2018
ae63752
add comments for yard docs
ianfixes Jan 25, 2018
7289056
update READMEs
ianfixes Jan 25, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ vendor
.rspec_status

# C++ stuff
arduino_ci_built.bin
*.bin
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ Style/StringLiterals:

Style/TrailingCommaInLiteral:
Enabled: false

Style/SymbolArray:
Enabled: false
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ language: ruby
# - rbx
# - "2.5.0"

before_install: gem install bundler -v 1.15.4
#before_install: gem install bundler -v 1.15.4
script:
- bundle install
- bundle exec rubocop --version
- bundle exec rubocop -D .
- bundle exec rspec
- bundle exec ci_system_check.rb
- cd SampleProjects/DoSomething
- bundle install
- bundle exec arduino_ci_remote.rb
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Unit testing support
- Documentation for all Ruby methods
- `ArduinoInstallation` class for managing lib / executable paths
- `DisplayManager` class for managing Xvfb instance if needed
- `ArduinoCmd` captures and caches preferences
Expand All @@ -15,6 +17,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `ArduinoCmd` installs libraries
- `ArduinoCmd` selects boards (compiler preference)
- `ArduinoCmd` verifies sketches
- `CppLibrary` manages GCC for unittests
- `CIConfig` manages overridable config for all testing

### Changed
- `DisplayManger.with_display` doesn't `disable` if the display was enabled prior to starting the block
Expand Down
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,38 @@
[Arduino CI](https://github.com/ifreecarve/arduino_ci) is a Ruby gem for executing Continuous Integration (CI) tests on an Arduino library -- both locally and as part of a service like Travis CI.


## Installation
## Installation In Your GitHub Project And Using Travis CI

Add this line to your application's Gemfile:
Add a file called `Gemfile` (no extension) to your Arduino project:

```ruby
source 'https://rubygems.org'
gem 'arduino_ci'
```

And then execute:
Next, you need this in `.travis.yml`

$ bundle
```yaml
sudo: false
language: ruby
script:
- bundle install
- bundle exec arduino_ci_remote.rb
```

That's literally all there is to it on the repository side. You'll need to go to https://travis-ci.org/profile/ and enable testing for your Arduino project. Once that happens, you should be all set.

Or install it yourself as:

$ gem install arduino_ci
## More Documentation

This software is in alpha. But [SampleProjects/DoSomething](SampleProjects/DoSomething) has a decent writeup and is a good bare-bones example of all the features.

## Usage
## Known Problems

TODO: Write usage instructions here, based on other TODO of writing the actual gem.
* The Arduino library is not fully mocked.
* I don't have preprocessor defines for all the Arduino board flavors
* Arduino Zero boards don't work in CI. I'm confused.
* https://github.com/ifreecarve/arduino_ci/issues


## Author
Expand All @@ -37,3 +49,5 @@ This gem was written by Ian Katz ([email protected]) in 2018. It's released
## See Also

* [Contributing](CONTRIBUTING.md)
* [Adafruit/travis-ci-arduino](https://github.com/adafruit/travis-ci-arduino) which inspired this project
* [mmurdoch/arduinounit](https://github.com/mmurdoch/arduinounit) from which the unit test macros were adopted
2 changes: 2 additions & 0 deletions SampleProjects/DoSomething/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ PATH
remote: /Users/ikatz/Development/non-wayfair/arduino_ci
specs:
arduino_ci (0.0.1)
os (~> 1.0)

GEM
remote: https://rubygems.org/
specs:
os (1.0.0)

PLATFORMS
ruby
Expand Down
100 changes: 94 additions & 6 deletions SampleProjects/DoSomething/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Arduino CI and Unit Tests HOWTO [![Build Status](https://travis-ci.org/ifreecarve/arduino-ci-unit-tests.svg?branch=master)](https://travis-ci.org/ifreecarve/arduino-ci-unit-tests)
# Arduino CI and Unit Tests HOWTO

This project is a template for a CI-enabled (and unit testable) Arduino project of your own.

Expand All @@ -7,17 +7,105 @@ This project is a template for a CI-enabled (and unit testable) Arduino project

* Travis CI
* Unit tests
* Development workflow matches CI workflow - the `platformio ci` line at the bottom of `.travis.yml` can be run on your local terminal (just append the name of the file you want to compile).
* Development workflow matches CI workflow

# Where The Magic Happens

Here is the minimal set of files that you will need to adapt to your own project:

* `.travis.yml` - You'll need to fill in the `env` section with files relevant to your project, and list out all the `--board`s under the `script` section.
* `platformio.ini` - You'll need to add information for any architectures you plan to support.
* `library.properties` - You'll need to update the `architectures` and `includes` lines as appropriate

### `Gemfile` Ruby gem configuration

```ruby
source 'https://rubygems.org'
gem 'arduino_ci'
```

You'll need this to get access to the functionality.

> This is different from the `Gemfile` that's included in this directory! That's for purposes of testing the ruby gem that also lives in this repository. So "do as I say, not as I do", in this case.


### `.travis.yml` Travis CI configuration

At a minimum, you will need the following lines in your file:

```yaml
language: ruby
script:
- bundle install
- bundle exec arduino_ci_remote.rb
```

This will install the necessary ruby gem, and run it. There are no command line arguments as of this writing, because all configuration is provided by...

### `.arduino-ci.yaml` ArduinoCI configuration

This file controls all aspects of building and unit testing. The (relatively-complete) defaults can be found [in the base project](../../misc/default.yaml).

The most relevant sections for most projects will be as follows:

```yaml
compile:
libraries: ~
platforms:
- uno
- due
- leonardo

unittest:
libraries: ~
platforms:
- uno
- due
- leonardo
```

This does nothing but specify a list of what platforms should run each set of tests. The platforms themselves can also be defined and/or extended in the yaml file. For example, `esp8266` as shown here:

```yaml
platforms:
esp8266:
board: esp8266:esp8266:huzzah
package: esp8266:esp8266
gcc:
features:
defines:
warnings:
flags:
```

Of course, this wouldn't work by itself -- the Arduino IDE would have to know how to install the package via the board manager. So there's a section for packages too:

```yaml
packages:
esp8266:esp8266:
url: http://arduino.esp8266.com/stable/package_esp8266com_index.json
```

### Unit tests in `test/`

All `.cpp` files in the `test/` directory are assumed to contain unit tests. Each and every one will be compiled and executed on its own.

The most basic unit test file is as follows:

```C++
#include <ArduinoUnitTests.h>
#include "../do-something.h"

unittest(your_test_name)
{
assertEqual(4, doSomething());
}

int main(int argc, char *argv[]) {
return Test::run_and_report(argc, argv);
}
```

This test defines one `unittest` (a macro provided by `ArduionUnitTests.h`), called `your_test_name`, which makes some assertions on the target library. The `int main` section is boilerplate.


# Credits

This Arduino example was created in January 2018 by Ian Katz <ianfixes@gmail.com>.
This Arduino example was created in January 2018 by Ian Katz <ifreecarve@gmail.com>.
2 changes: 1 addition & 1 deletion SampleProjects/DoSomething/do-something.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <Arduino.h>
#include <do-something.h>
#include "do-something.h"
int doSomething(void) {
millis(); // this line is only here to test that we're able to refer to the builtins
return 4;
Expand Down
1 change: 1 addition & 0 deletions SampleProjects/DoSomething/do-something.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#pragma once
#include <Arduino.h>
int doSomething(void);
4 changes: 2 additions & 2 deletions SampleProjects/DoSomething/library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=arduino-ci-unit-tests
name=DoSomething
version=0.1.0
author=Ian Katz <[email protected]>
maintainer=Ian Katz <[email protected]>
sentence=Arduino CI unit test example
paragraph=A skeleton library demonstrating CI and unit tests
category=Other
url=https://github.com/ifreecarve/arduino-ci-unit-tests
url=https://github.com/ifreecarve/arduino_ci/SampleProjects/DoSomething
architectures=avr
includes=do-something.h
11 changes: 11 additions & 0 deletions SampleProjects/DoSomething/test/good-library.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <ArduinoUnitTests.h>
#include "../do-something.h"

unittest(library_does_something)
{
assertEqual(4, doSomething());
}

int main(int argc, char *argv[]) {
return Test::run_and_report(argc, argv);
}
24 changes: 24 additions & 0 deletions SampleProjects/DoSomething/test/good-null.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <ArduinoUnitTests.h>

unittest(equality_as_vars)
{
int x = 3;
int y = 3;
int z = 3;
assertEqual(x, y);
assertEqual(x, z);
}

unittest(equality_as_values)
{
assertEqual(1, 1);
assertEqual(4, 4);
}

unittest(nothing)
{
}

int main(int argc, char *argv[]) {
return Test::run_and_report(argc, argv);
}
2 changes: 1 addition & 1 deletion SampleProjects/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Arduino Sample Projects
=======================

This directory contains example projects that are meant to be built with this gem.
This directory contains example projects that are meant to be built with this gem. Except "TestSomething". That one's just a beater for CI testing and includes some tests that are designed to fail (to test negative inputs).
2 changes: 2 additions & 0 deletions SampleProjects/TestSomething/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gem 'arduino_ci', path: '../../'
19 changes: 19 additions & 0 deletions SampleProjects/TestSomething/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
PATH
remote: /Users/ikatz/Development/non-wayfair/arduino_ci
specs:
arduino_ci (0.0.1)
os (~> 1.0)

GEM
remote: https://rubygems.org/
specs:
os (1.0.0)

PLATFORMS
ruby

DEPENDENCIES
arduino_ci!

BUNDLED WITH
1.16.0
3 changes: 3 additions & 0 deletions SampleProjects/TestSomething/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# TestSomething

This is a "beater" example that is referenced by tests of the Arduino CI module itself.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <test-something.h>
// if it seems bare, that's because it's only meant to
// demonstrate compilation -- that references work
void setup() {
}

void loop() {
testSomething();
}
10 changes: 10 additions & 0 deletions SampleProjects/TestSomething/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=TestSomething
version=0.1.0
author=Ian Katz <[email protected]>
maintainer=Ian Katz <[email protected]>
sentence=Arduino CI unit test example
paragraph=A skeleton library demonstrating CI and unit tests
category=Other
url=https://github.com/ifreecarve/arduino_ci/SampleProjects/TestSomething
architectures=avr,esp8266
includes=do-something.h
5 changes: 5 additions & 0 deletions SampleProjects/TestSomething/test-something.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "test-something.h"
int testSomething(void) {
millis(); // this line is only here to test that we're able to refer to the builtins
return 4;
};
3 changes: 3 additions & 0 deletions SampleProjects/TestSomething/test-something.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
#include <Arduino.h>
int testSomething(void);
12 changes: 12 additions & 0 deletions SampleProjects/TestSomething/test/bad-null.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <ArduinoUnitTests.h>

unittest(pretend_equal_things_arent)
{
int x = 3;
int y = 3;
assertNotEqual(x, y);
}

int main(int argc, char *argv[]) {
return Test::run_and_report(argc, argv);
}
11 changes: 11 additions & 0 deletions SampleProjects/TestSomething/test/good-library.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <ArduinoUnitTests.h>
#include "../test-something.h"

unittest(library_tests_something)
{
assertEqual(4, testSomething());
}

int main(int argc, char *argv[]) {
return Test::run_and_report(argc, argv);
}
Loading