{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "
\n", " \n", " \"QuantEcon\"\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Packages, Testing, and Continuous Integration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contents\n", "\n", "- [Packages, Testing, and Continuous Integration](#Packages,-Testing,-and-Continuous-Integration) \n", " - [Project Setup](#Project-Setup) \n", " - [Project Structure](#Project-Structure) \n", " - [Project Workflow](#Project-Workflow) \n", " - [Unit Testing](#Unit-Testing) \n", " - [Continuous Integration with Travis](#Continuous-Integration-with-Travis) \n", " - [Code Coverage](#Code-Coverage) \n", " - [Pull Requests to External Julia Projects](#Pull-Requests-to-External-Julia-Projects) \n", " - [Benchmarking](#Benchmarking) \n", " - [Additional Notes](#Additional-Notes) \n", " - [Review](#Review) \n", " - [Exercises](#Exercises) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> A complex system that works is invariably found to have evolved from a simple system that worked. The inverse proposition also appears to be true: A complex system designed from scratch never works and cannot be made to work. You have to start over, beginning with a working simple system – [Gall’s Law](https://en.wikipedia.org/wiki/John_Gall_%28author%29#Gall%27s_law).\n", "\n", "\n", "Co-authored with Arnav Sood\n", "\n", "This lecture discusses structuring a project as a Julia module, and testing it with tools from GitHub.\n", "\n", "Benefits include\n", "\n", "- Specifying dependencies (and their versions) so that your project works across Julia setups and over time. \n", "- Being able to load your project’s functions from outside without copy/pasting. \n", "- Writing tests that run locally, *and automatically on the GitHub server*. \n", "- Having GitHub test your project across operating systems, Julia versions, etc. \n", "\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Project Setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Account Setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Travis CI\n", "\n", "As we’ll see later, Travis is a service that automatically tests your project on the GitHub server.\n", "\n", "First, we need to make sure that your GitHub account is set up with Travis CI and Codecov.\n", "\n", "As a reminder, make sure you signed up for the GitHub [Student Developer Pack](https://education.github.com/pack/) or [Academic Plan](https://help.github.com/articles/applying-for-an-academic-research-discount/) if eligible.\n", "\n", "Navigate to the [travis-ci.com website](https://travis-ci.com/) and click “sign up with GitHub” – supply your credentials.\n", "\n", "If you get stuck, see the [Travis tutorial](https://docs.travis-ci.com/user/tutorial/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Codecov\n", "\n", "Codecov is a service that tells you how comprehensive your tests are (i.e., how much of your code is actually tested).\n", "\n", "To sign up, visit the [Codecov website](http://codecov.io/), and click “sign up”\n", "\n", "\n", "\n", " \n", "Next, click “add a repository” and *enable private scope* (this allows Codecov to service your private projects).\n", "\n", "The result should be\n", "\n", "\n", "\n", " \n", "This is all we need for now." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Julia Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide-output": true }, "outputs": [], "source": [ "using InstantiateFromURL\n", "# optionally add arguments to force installation: instantiate = true, precompile = true\n", "github_project(\"QuantEcon/quantecon-notebooks-julia\", version = \"0.8.0\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** Before these steps, make sure that you’ve either completed the [version control](version_control.html) lecture or run." ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "git config --global user.name \"Your Name\"\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** Throughout this lecture, important points and sequential workflow steps are listed as bullets.\n", "\n", "To set up a project on Julia:\n", "\n", "- Load the [PkgTemplates](https://github.com/invenia/PkgTemplates.jl/) package. " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "using PkgTemplates\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Create a *template* for your project. \n", "\n", "\n", "This specifies metadata like the license we’ll be using (MIT by default), the location (`~/.julia/dev` by default), etc." ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "ourTemplate = Template(;user=\"quanteconuser\", plugins = [TravisCI(), Codecov()], manifest = true)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note**: Make sure you replace the `quanteconuser` with your GitHub ID.\n", "\n", "- Create a specific project based off this template " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "generate(\"ExamplePackage.jl\", ourTemplate)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we navigate to the package directory, we should see something like the following.\n", "\n", "\n", "\n", " \n", "As a reminder, the location of your `.julia` folder can be found by running `DEPOT_PATH[1]` in a REPL .\n", "\n", "**Note:** On Mac, this may be hidden; you can either start a terminal, `cd ~` and then `cd .julia`, or make [hidden files visible](https://ianlunn.co.uk/articles/quickly-showhide-hidden-files-mac-os-x-mavericks/) in the Finder." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding a Project to Git\n", "\n", "The next step is to add this project to Git version control.\n", "\n", "- Open the repository screen in your account as discussed previously. \n", "\n", "\n", "We’ll want the following settings\n", "\n", "\n", "\n", " \n", "In particular\n", "\n", "- The repo you create should have the same name as the project we added. \n", "- We should leave the boxes unchecked for the `README.md`, `LICENSE`, and `.gitignore`, since these are handled by `PkgTemplates`. \n", "\n", "\n", "Then,\n", "\n", "- Drag and drop your folder from your `~/.julia/dev` directory to GitHub Desktop. \n", "- Click the “publish branch” button to upload your files to GitHub. \n", "\n", "\n", "If you navigate to your git repo (ours is [here](https://github.com/quanteconuser/ExamplePackage.jl/)), you should see something like\n", "\n", "\n", "\n", " \n", "**Note:** Be sure that you don’t separately clone the repo you just added to another location (i.e., to your desktop).\n", "\n", "A key note is that you have some set of files on your local machine (here in `~/.julia/dev/ExamplePackage.jl`) and git is plugged into those files.\n", "\n", "For convenience, you might want to create a shortcut to that location somewhere accessible." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding a Project to the Julia Package Manager\n", "\n", "We also want Julia’s package manager to be aware of the project.\n", "\n", "- Open a REPL in the newly created project directory, either by noting the path printed above, or by running the following in a REPL. " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "cd(joinpath(DEPOT_PATH[1], \"dev\", \"ExamplePackage\"))\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note the lack of `.jl`!\n", "\n", "- Run the following " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] activate\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "to get into the main Julia environment (more on environments in the second half of this lecture).\n", "\n", "- And run " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] dev .\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "to add the package.\n", "\n", "You can see the change reflected in our default package list by running" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] st\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more on the package mode, see the [tools and editors](tools_editors.html) lecture." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using the Package Manager\n", "\n", "Now, from any Julia terminal in the future, we can run" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```text\n", "using ExamplePackage\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use its exported functions.\n", "\n", "We can also get the path to this by running" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```text\n", "using ExamplePackage\n", "pathof(ExamplePackage) # returns path to src/ExamplePackage.jl\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Project Structure\n", "\n", "Let’s unpack the structure of the generated project\n", "\n", "- The first directory, `.git`, holds the version control information. \n", "- The `src` directory contains the project’s source code – it should contain only one file (`ExamplePackage.jl`), which reads " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```text\n", "module ExamplePackage\n", "\n", "greet() = print(\"Hello World!\")\n", "\n", "end # module\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Likewise, the `test` directory should have only one file (`runtests.jl`), which reads " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```text\n", "using ExamplePackage\n", "using Test\n", "\n", "@testset \"ExamplePackage.jl\" begin\n", " # Write your own tests here.\n", "end\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "In particular, the workflow is to export objects we want to test (`using ExamplePackage`), and test them using Julia’s `Test` module.\n", "\n", "The other important text files for now are\n", "\n", "- `Project.toml` and `Manifest.toml`, which contain dependency information. \n", "\n", "\n", "In particular, the `Project.toml` contains a list of dependencies, and the `Manifest.toml` specifies their exact versions and sub-dependencies.\n", "\n", "- The `.gitignore` file (which may display as an untitled file), which contains files and paths for `git` to ignore. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Project Workflow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dependency Management" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Environments\n", "\n", "As [before](tools_editors.html#jl-packages), the .toml files define an *environment* for our project, or a set of files which represent the dependency information.\n", "\n", "The actual files are written in the [TOML language](https://github.com/toml-lang/toml), which is a lightweight format to specify configuration options.\n", "\n", "This information is the name of every package we depend on, along with the exact versions of those packages.\n", "\n", "This information (in practice, the result of package operations we execute) will\n", "be reflected in our `ExamplePackage.jl` directory’s TOML, once that environment is activated (selected).\n", "\n", "This allows us to share the project with others, who can exactly reproduce the state used to build and test it.\n", "\n", "See the [Pkg3 docs](https://docs.julialang.org/en/v1/stdlib/Pkg/) for more information." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Pkg Operations\n", "\n", "For now, let’s just try adding a dependency\n", "\n", "- Activate the package environment (to be run from the base, `v1.1` environment) " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] activate ExamplePackage\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
This tells Julia to write the results of package operations to `ExampleProject`’s TOML,
\n", "
\n", "and use the versions of packages specified there.\n", "\n", "
\n", "\n", "
\n", "\n", "Note that the base environment isn’t special, except that it’s what’s loaded by a freshly-started REPL or Jupyter notebook.\n", "\n", "- Add a package " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] add Expectations\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can track changes in the TOML, as before.\n", "\n", "Here’s the `Manifest.toml`\n", "\n", "\n", "\n", " \n", "We can also run other operations, like `] up`, `] precompile`, etc.\n", "\n", "Package operations are listed in detail in the [tools and editors](tools_editors.html) lecture.\n", "\n", "Recall that, to quit the active environment and return to the base `(v1.1)`, simply run" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] activate\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Writing Code\n", "\n", "The basic idea is to work in `tests/runtests.jl`, while reproducible functions should go in the `src/ExamplePackage.jl`.\n", "\n", "For example, let’s say we add `Distributions.jl`" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] activate ExamplePackage\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] add Distributions\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and edit the source (paste this into the file itself ) to read as follows" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "module ExamplePackage\n", "\n", "greet() = print(\"Hello World!\")\n", "\n", "using Expectations, Distributions\n", "\n", "function foo(μ = 1., σ = 2.)\n", " d = Normal(μ, σ)\n", " E = expectation(d)\n", " return E(x -> sin(x))\n", "end\n", "\n", "export foo\n", "\n", "end # module\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let’s try calling this" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] activate\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "using ExamplePackage\n", "ExamplePackage.greet()\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "foo() # exported, so don't need to qualify the namespace\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** If you didn’t follow the instructions to add a [startup file](tools_editors.html#jl-startup-file), you may need to quit your REPL and load the package again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Jupyter Workflow\n", "\n", "We can also work with the package from a Jupyter notebook.\n", "\n", "Let’s create a new output directory in our project, and run `jupyter lab` from it.\n", "\n", "Create a new notebook `output.ipynb`\n", "\n", "\n", "\n", " \n", "From here, we can use our package’s functions as we would functions from other packages.\n", "\n", "This lets us produce neat output documents, without pasting the whole codebase.\n", "\n", "We can also run package operations inside the notebook\n", "\n", "\n", "\n", " \n", "The change will be reflected in the `Project.toml` file.\n", "\n", "Note that, as usual, we had to first activate `ExamplePackage` first before making our dependency changes" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "name = \"ExamplePackage\"\n", "uuid = \"f85830d0-e1f0-11e8-2fad-8762162ab251\"\n", "authors = [\"QuantEcon User \"]\n", "version = \"0.1.0\"\n", "\n", "[deps]\n", "Distributions = \"31c24e10-a181-5473-b8eb-7969acd0382f\"\n", "Expectations = \"2fe49d83-0758-5602-8f54-1f90ad0d522b\"\n", "Parameters = \"d96e819e-fc66-5662-9728-84c9c7592b0a\"\n", "\n", "[extras]\n", "Test = \"8dfed614-e22c-5e08-85e1-65c5234f0b40\"\n", "\n", "[targets]\n", "test = [\"Test\"]\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There will also be changes in the Manifest as well .\n", "\n", "Be sure to add `output/.ipynb_checkpoints` to your `.gitignore` file, so that’s not checked in.\n", "\n", "Make sure you’ve activated the project environment (`] activate ExamplePackage`) before you try to propagate changes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Collaborative Work\n", "\n", "For someone else to get the package, they simply need to\n", "\n", "- Run the following command " ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] dev https://github.com/quanteconuser/ExamplePackage.jl.git\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will place the repository inside their `~/.julia/dev` folder.\n", "\n", "- Drag-and-drop the folder to GitHub desktop in the usual way. \n", "\n", "\n", "Recall that the path to your `~/.julia` folder is" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\"/home/ubuntu/.julia\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DEPOT_PATH[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "They can then collaborate as they would on other git repositories.\n", "\n", "In particular, they can run" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] activate ExamplePackage\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "to load the list of dependencies, and" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] instantiate\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "to make sure they are installed on the local machine." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Testing\n", "\n", "It’s important to make sure that your code is well-tested.\n", "\n", "There are a few different kinds of test, each with different purposes\n", "\n", "- *Unit testing* makes sure that individual pieces of a project work as expected. \n", "- *Integration testing* makes sure that they fit together as expected. \n", "- *Regression testing* makes sure that behavior is unchanged over time. \n", "\n", "\n", "In this lecture, we’ll focus on unit testing.\n", "\n", "In general, well-written unit tests (which also guard against regression, for example by comparing function output to hardcoded values) are sufficient for most small projects." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `Test` Module\n", "\n", "Julia provides testing features through a built-in package called `Test`, which we get by `using Test`.\n", "\n", "The basic object is the macro `@test`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[32m\u001b[1mTest Passed\u001b[22m\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Test\n", "@test 1 == 1\n", "@test 1 ≈ 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tests will pass if the condition is `true`, or fail otherwise.\n", "\n", "If a test is failing, we should flag it with `@test_broken` as below" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[33m\u001b[1mTest Broken\u001b[22m\u001b[39m\n", " Expression: 1 == 2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@test_broken 1 == 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This way, we still have access to information about the test, instead of just deleting it or commenting it out.\n", "\n", "There are other test macros, that check for things like error handling and type-stability.\n", "\n", "Advanced users can check the [Julia docs](https://docs.julialang.org/en/v1/stdlib/Test/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example\n", "\n", "Let’s add some unit tests for the `foo()` function we defined earlier.\n", "\n", "Our `tests/runtests.jl` file should look like this.\n", "\n", "**As before, this should be pasted into the file directly**" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "using ExamplePackage\n", "using Test\n", "\n", "@test foo() ≈ 0.11388071406436832\n", "@test foo(1, 1.5) ≈ 0.2731856314283442\n", "@test_broken foo(1, 0) # tells us this is broken\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And run it by typing `] test` into an activated REPL (i.e., a REPL where you’ve run `] activate ExamplePackage`)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test Sets\n", "\n", "By default, the `runtests.jl` folder starts off with a `@testset`.\n", "\n", "This is useful for organizing different batches of tests, but for now we can simply ignore it.\n", "\n", "To learn more about test sets, see [the docs](https://docs.julialang.org/en/v1/stdlib/Test/index.html#Working-with-Test-Sets-1/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running Tests\n", "\n", "There are a few different ways to run the tests for your package.\n", "\n", "- Run the actual `runtests.jl`, say by hitting `shift-enter` on it in Atom. \n", "- From a fresh (`v1.1`) REPL, run `] test ExamplePackage`. \n", "- From an activated (`ExamplePackage`) REPL, simply run `] test` (recall that you can activate with `] activate ExamplePackage`). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continuous Integration with Travis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup\n", "\n", "By default, Travis should have access to all your repositories and deploy automatically.\n", "\n", "This includes private repos if you’re on a student developer pack or an academic plan (Travis detects this automatically).\n", "\n", "To change this, go to “settings” under your GitHub profile\n", "\n", "\n", "\n", " \n", "Click “Applications,” then “Travis CI,” then “Configure,” and choose the repos you want to be tracked." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Build Options\n", "\n", "By default, Travis will compile and test your project (i.e., “build” it) for new commits and PRs for every tracked repo with a `.travis.yml` file.\n", "\n", "We can see ours by opening it in Atom" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "# Documentation: http://docs.travis-ci.com/user/languages/julia/\n", "language: julia\n", "os:\n", "- linux\n", "- osx\n", "julia:\n", "- 1.1\n", "- nightly\n", "matrix:\n", "allow_failures:\n", " - julia: nightly\n", "fast_finish: true\n", "notifications:\n", "email: false\n", "after_success:\n", "- julia -e 'using Pkg; Pkg.add(\"Coverage\"); using Coverage; Codecov.submit(process_folder())'\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is telling Travis to build the project in Julia, on OSX and Linux, using Julia v1.1 and the latest development build (“nightly”).\n", "\n", "It also says that if the nightly version doesn’t work, that shouldn’t register as a failure.\n", "\n", "**Note** You won’t need OSX unless you’re building something Mac-specific, like iOS or Swift.\n", "\n", "You can delete those lines to speed up the build, likewise for the nightly Julia version." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working with Builds\n", "\n", "As above, builds are triggered whenever we push changes or open a pull request.\n", "\n", "For example, if we push our changes to the server and then click the Travis badge (the one which says “build”) on the README, we should see something like\n", "\n", "\n", "\n", " \n", "Note that you may need to wait a bit and/or refresh your browser.\n", "\n", "This gives us an overview of all the builds running for that commit.\n", "\n", "To inspect a build more closely (say, if it fails), we can click on it and expand the log options\n", "\n", "\n", "\n", " \n", "Note that the build times here aren’t informative, because we can’t generally control the hardware to which our job is allocated.\n", "\n", "We can also cancel specific jobs, either from their specific pages or by clicking the grey “x” button on the dashboard.\n", "\n", "Lastly, we can trigger builds manually (without a new commit or PR) from the Travis overview\n", "\n", "\n", "\n", " \n", "To commit *without* triggering a build, simply add “[ci skip]” somewhere inside the commit message." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Travis and Pull Requests\n", "\n", "One key feature of Travis is the ability to see at-a-glance whether PRs pass tests before merging them.\n", "\n", "This happens automatically when Travis is enabled on a repository.\n", "\n", "For an example of this feature, see [this PR](https://github.com/QuantEcon/Games.jl/pull/65/) in the `Games.jl` repository." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code Coverage\n", "\n", "Beyond the success or failure of our test suite, we also want to know how much of our code the tests cover.\n", "\n", "The tool we use to do this is called [Codecov](http://codecov.io)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup\n", "\n", "You’ll find that Codecov is automatically enabled for public repos with Travis.\n", "\n", "For private ones, you’ll need to first get an access token.\n", "\n", "Add private scope in the Codecov website, just like we did for Travis.\n", "\n", "Navigate to the repo settings page (i.e., `https://codecov.io/gh/quanteconuser/ExamplePackage.jl/settings` for our repo) and copy the token.\n", "\n", "Next, go to your Travis settings and add an environment variable as below\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interpreting Results\n", "\n", "Click the Codecov badge to see the build page for your project.\n", "\n", "This shows us that our tests cover 50% of our functions in `src//`.\n", "\n", "**Note:** To get a more detailed view, we can click the `src//` and the resultant filename.\n", "\n", "**Note:** Codecov may take a few minutes to run for the first time\n", "\n", "\n", "\n", " \n", "This shows us precisely which methods (and parts of methods) are untested." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pull Requests to External Julia Projects\n", "\n", "As mentioned in [version control](version_control.html), sometimes we’ll want to work on external repos that are also Julia projects.\n", "\n", "- `] dev` the git URL (or package name, if the project is a registered Julia package), which will both clone the git repo to `~/.julia/dev` and sync it with the Julia package manager. \n", "\n", "\n", "For example, running" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] dev Expectations\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "will clone the repo `https://github.com/quantecon/Expectations.jl` to `~/.julia/dev/Expectations`.\n", "\n", "Make sure you do this from the base Julia environment (i.e., after running `] activate` without arguments).\n", "\n", "As a reminder, you can find the location of your `~/.julia` folder (called the “user depot”), by running" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "\"/home/ubuntu/.julia\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DEPOT_PATH[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `] dev` command will also add the target to the package manager, so that whenever we run `using Expectations`, Julia will load our cloned copy from that location" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "using Expectations\n", "pathof(Expectations) # points to our git clone\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Drag that folder to GitHub Desktop. \n", "- The next step is to fork the original (external) package from its website (i.e., `https://github.com/quantecon/Expectations.jl`) to your account (`https://github.com/quanteconuser/Expectations.jl` in our case). \n", " \n", " \n", " \n", " \n", " \n", "- Edit the settings in GitHub Desktop (from the “Repository” dropdown) to reflect the new URL. \n", " \n", " \n", " \n", " \n", " Here, we’d change the highlighted text to read `quanteconuser`, or whatever our GitHub ID is. \n", " \n", "- If you make some changes in a text editor and return to GitHub Desktop, you’ll see something like. \n", "\n", "\n", "**Note:** As before, we’re editing the files directly in `~/.julia/dev`, as opposed to cloning the repo again.\n", "\n", "\n", "\n", " \n", "Here, for example, we’re revising the README.\n", "\n", "- Clicking “commit to master” (recall that the checkboxes next to each file indicate whether it’s to be committed) and then pushing (e.g., hitting “push” under the “Repository” dropdown) will add the committed changes to your account. \n", "\n", "\n", "To confirm this, we can check the history on our account [here](https://github.com/quanteconuser/Expectations.jl/commits/master); for more on working with git repositories, see the [version control](version_control.html) lecture.\n", "\n", "\n", "\n", " \n", "The green check mark indicates that Travis tests passed for this commit.\n", "\n", "- Clicking “new pull request” from the pull requests tab will show us a snapshot of the changes, and let us create a pull request for project maintainers to review and approve. \n", "\n", "\n", "\n", "\n", " \n", "For more on PRs, see the relevant section of the [version control](version_control.html) lecture.\n", "\n", "For more on forking, see the docs on [GitHub Desktop](https://help.github.com/desktop/guides/contributing-to-projects/cloning-a-repository-from-github-to-github-desktop/) and [the GitHub Website](https://guides.github.com/activities/forking/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Case with Write Access\n", "\n", "If you have write access to the repo, we can skip the preceding steps about forking and changing the URL.\n", "\n", "You can use `] dev` on a package name or the URL of the package" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] dev Expectations\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or `] dev https://github.com/quanteconuser/Expectations.jl.git` as an example for an unreleased package by URL.\n", "\n", "Which will again clone the repo to `~/.julia/dev`, and use it as a Julia package." ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "using Expectations\n", "pathof(Expectations) # points to our git clone\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, drag that folder to GitHub Desktop as before.\n", "\n", "Then, in order to work with the package locally, all we need to do is open the `~/.julia/dev/Expectations` in a text editor (like Atom)\n", "\n", "\n", "\n", " \n", "From here, we can edit this package just like we created it ourselves and use GitHub Desktop to track versions of our package files (say, after `] up`, or editing source code, `] add Package`, etc.)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Removing a Julia Package\n", "\n", "To “un-dev” a Julia package (say, if we want to use our old `Expectations.jl`), you can simply run" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] free Expectations\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To delete it entirely, simply run" ] }, { "cell_type": "markdown", "metadata": { "hide-output": false }, "source": [ "```julia\n", "] rm Expectations\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From a REPL where that package is in the active environment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Benchmarking\n", "\n", "Another goal of testing is to make sure that code doesn’t slow down significantly from one version to the next.\n", "\n", "We can do this using tools provided by the `BenchmarkTools.jl` package.\n", "\n", "See the [need for speed](need_for_speed.html) lecture for more details." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Additional Notes\n", "\n", "- The [JuliaCI](https://github.com/JuliaCI/) organization provides more Julia utilities for continuous integration and testing. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Review\n", "\n", "To review the workflow for creating, versioning, and testing a new project end-to-end.\n", "\n", "1. Create the local package directory using the `PkgTemplates.jl`. \n", "1. Add that package to the Julia package manager, by opening a Julia REPL in the `~/.julia/dev/ExamplePackage.jl`, making sure the active environment is the default one `(v1.1)`, and hitting `] dev .`. \n", "1. Drag-and-drop that folder to GitHub Desktop. \n", "1. Create an empty repository with the same name on the GitHub server. \n", "1. Push from GitHub Desktop to the server. \n", "1. Open **the original project folder** (e.g., `~/.julia/dev/ExamplePackage.jl`) in Atom. \n", "1. Make changes, test, iterate on it, etc. As a rule, functions like should live in the `src/` directory once they’re stable, and you should export them from that file with `export func1, func2`. This will export all methods of `func1`, `func2`, etc. \n", "1. Commit them in GitHub Desktop as you go (i.e., you can and should use version control to track intermediate states). \n", "1. Push to the server, and see the Travis and Codecov results (note that these may take a few minutes the first time). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1\n", "\n", "Following the [instructions for a new project](#project-setup), create a new package on your github account called `NewtonsMethod.jl`.\n", "\n", "In this package, you should create a simple package to do Newton’s Method using the code you did in the\n", "[Newton’s method](../getting_started_julia/julia_by_example.html#jbe-ex8a) exercise in [Introductory Examples](../getting_started_julia/julia_by_example.html).\n", "\n", "In particular, within your package you should have two functions\n", "\n", "- `newtonroot(f, f′; x₀, tol = 1E-7, maxiter = 1000)` \n", "- `newtonroot(f; x₀, tol = 1E-7, maxiter = 1000)` \n", "\n", "\n", "Where the second function uses Automatic Differentiation to call the first.\n", "\n", "The package should include\n", "\n", "- implementations of those functions in the `/src` directory \n", "- comprehensive set of tests \n", "- project and manifest files to replicate your development environment \n", "- automated running of the tests with Travis CI in GitHub \n", "\n", "\n", "For the tests, you should have at the very minimum\n", "\n", "- a way to handle non-convergence (e.g. return back `nothing` as discussed in [error handling](../getting_started_julia/fundamental_types.html#error-handling) \n", "- several `@test` for the root of a known function, given the `f` and analytical `f'` derivatives \n", "- tests of those roots using the automatic differentiation version of the function \n", "- test of finding those roots with a `BigFloat` and not just a `Float64` \n", "- test of non-convergence for a function without a root (e.g. $ f(x) = 2 + x^2 $ ) \n", "- test to ensure that the `maxiter` is working (e.g. what happens if you call `maxiter = 5` \n", "- test to ensure that `tol` is working \n", "\n", "\n", "And anything else you can think of. You should be able to run `] test` for the project to check that the test-suite is running, and then ensure that it is running automatically on Travis CI.\n", "\n", "Push a commit to the repository which breaks one of the tests and see what the Travis CI reports after running the build." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2\n", "\n", "Watch the youtube video [Developing Julia Packages](https://www.youtube.com/watch?v=QVmU29rCjaA) from Chris Rackauckas. The demonstration goes through many of the same concepts as this lecture, but with more background in test-driven development and providing more details for open-source projects.." ] } ], "metadata": { "date": 1591310624.2311802, "download_nb": 1, "download_nb_path": "https://julia.quantecon.org/", "filename": "testing.rst", "filename_with_path": "more_julia/testing", "kernelspec": { "display_name": "Julia 1.4.2", "language": "julia", "name": "julia-1.4" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.2" }, "title": "Packages, Testing, and Continuous Integration" }, "nbformat": 4, "nbformat_minor": 2 }