From f0d6d4b1d253616feaad085f8a405e0531b5f7d5 Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Thu, 27 Oct 2022 11:53:45 +0200 Subject: [PATCH 1/4] opgpcard: Add rudimentary logging to build.rs --- tools/build.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/build.rs b/tools/build.rs index 608f687..b3b1113 100644 --- a/tools/build.rs +++ b/tools/build.rs @@ -13,10 +13,12 @@ fn main() { if flagfile.exists() { subplot_build::codegen("subplot/opgpcard.subplot") .expect("failed to generate code with Subplot"); + println!("cargo:warning=generating subplot tests"); } else { // If we're not generating code from the subplot, we should at // least create an empty file so that the tests/opgpcard.rs // file can include it. Otherwise the build will fail. + println!("cargo:warning=flagfile not found"); let out_dir = std::env::var("OUT_DIR").unwrap(); let include = Path::new(&out_dir).join("opgpcard.rs"); eprintln!("build.rs: include={}", include.display()); From bfe22ec1b8189f29e76c1b1db3f9b9df57dcd5b4 Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Thu, 27 Oct 2022 11:54:17 +0200 Subject: [PATCH 2/4] opgpcard: Make shebang system independent --- tools/cargo-test-in-docker | 2 +- tools/subplot/test-in-docker.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/cargo-test-in-docker b/tools/cargo-test-in-docker index 945294f..e442984 100755 --- a/tools/cargo-test-in-docker +++ b/tools/cargo-test-in-docker @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # # Run this to run opgpcard (tools directory) test suite inside a # Docker container with a virtual smartcard running. The test suite diff --git a/tools/subplot/test-in-docker.sh b/tools/subplot/test-in-docker.sh index 13bf2e1..c24ae9a 100755 --- a/tools/subplot/test-in-docker.sh +++ b/tools/subplot/test-in-docker.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # # Run "cargo test" inside a Docker container with virtual cards # installed and running. This will allow at least rudimentary testing From db34132ddac466e0f0177de683f4b3f4d7a9bbce Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Thu, 27 Oct 2022 11:56:24 +0200 Subject: [PATCH 3/4] opgpcard: Make docker script more robust --- tools/cargo-test-in-docker | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/cargo-test-in-docker b/tools/cargo-test-in-docker index e442984..0d5e66c 100755 --- a/tools/cargo-test-in-docker +++ b/tools/cargo-test-in-docker @@ -20,4 +20,6 @@ sed -i "s/timeout=20/timeout=60/" /home/jcardsim/run-card.sh && su - -c "sh /home/jcardsim/run-card.sh >/dev/null" jcardsim && cd /src/tools && if ! [ -e virtual-card-available ]; then rm -f tests/opgpcard.rs; fi && +CARGO_TARGET_DIR=/cargo/ cargo update && +CARGO_TARGET_DIR=/cargo/ cargo build -vv && CARGO_TARGET_DIR=/cargo/ cargo test' From dfb7f3275d43a949cb4e4a0e84547d3da1e5efb0 Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Thu, 27 Oct 2022 11:48:21 +0200 Subject: [PATCH 4/4] opgpcard: Replace flagfile with environment variable - A flagfile requires manual management with touch and rm, replace it with an environment variable that can be set more conveniently. - Removing tools/tests/opgpcard.rs is not necessary, the file it links to is always generated in build.rs, either with subplot tests or without. But it is always there, so cargo test does not fail for that reason. --- .gitlab-ci.yml | 3 +-- tools/build.rs | 32 +++++++++++++++++--------------- tools/cargo-test-in-docker | 2 +- tools/subplot/opgpcard.md | 6 +++--- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a4a876a..f12d583 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -145,8 +145,7 @@ subplot: script: # make sure a virtual card is available, so that the subplot tests are # generated - - touch tools/virtual-card-available - - cargo test + - CARD_BASED_TESTS=true cargo test cache: # inherit all general cache settings <<: *general_cache_config diff --git a/tools/build.rs b/tools/build.rs index b3b1113..5185c03 100644 --- a/tools/build.rs +++ b/tools/build.rs @@ -9,21 +9,23 @@ fn main() { // card is available. This is a kludge until Subplot can do // conditional scenarios // (https://gitlab.com/subplot/subplot/-/issues/20). - let flagfile = Path::new("virtual-card-available"); - if flagfile.exists() { - subplot_build::codegen("subplot/opgpcard.subplot") - .expect("failed to generate code with Subplot"); - println!("cargo:warning=generating subplot tests"); - } else { - // If we're not generating code from the subplot, we should at - // least create an empty file so that the tests/opgpcard.rs - // file can include it. Otherwise the build will fail. - println!("cargo:warning=flagfile not found"); - let out_dir = std::env::var("OUT_DIR").unwrap(); - let include = Path::new(&out_dir).join("opgpcard.rs"); - eprintln!("build.rs: include={}", include.display()); - if !include.exists() { - File::create(include).unwrap(); + match option_env!("CARD_BASED_TESTS") { + Some(_) => { + subplot_build::codegen("subplot/opgpcard.subplot") + .expect("failed to generate code with Subplot"); + println!("cargo:warning=generated subplot tests"); + } + None => { + // If we're not generating code from the subplot, we should at + // least create an empty file so that the tests/opgpcard.rs + // file can include it. Otherwise the build will fail. + println!("cargo:warning=did not generate subplot tests"); + let out_dir = std::env::var("OUT_DIR").unwrap(); + let include = Path::new(&out_dir).join("opgpcard.rs"); + eprintln!("build.rs: include={}", include.display()); + if !include.exists() { + File::create(include).unwrap(); + } } } } diff --git a/tools/cargo-test-in-docker b/tools/cargo-test-in-docker index 0d5e66c..9332cd8 100755 --- a/tools/cargo-test-in-docker +++ b/tools/cargo-test-in-docker @@ -14,12 +14,12 @@ docker run --rm -it \ -v root:/root \ -v cargo:/cargo \ -v $(pwd):/src \ + -e CARD_BASED_TESTS=true \ registry.gitlab.com/openpgp-card/virtual-cards/smartpgp-builddeps sh -c ' sed -i "s/timeout=20/timeout=60/" /home/jcardsim/run-card.sh && /etc/init.d/pcscd start && su - -c "sh /home/jcardsim/run-card.sh >/dev/null" jcardsim && cd /src/tools && -if ! [ -e virtual-card-available ]; then rm -f tests/opgpcard.rs; fi && CARGO_TARGET_DIR=/cargo/ cargo update && CARGO_TARGET_DIR=/cargo/ cargo build -vv && CARGO_TARGET_DIR=/cargo/ cargo test' diff --git a/tools/subplot/opgpcard.md b/tools/subplot/opgpcard.md index de1693f..d0729d8 100644 --- a/tools/subplot/opgpcard.md +++ b/tools/subplot/opgpcard.md @@ -17,9 +17,9 @@ The verification scenarios in this document assume the availability of a virtual smart card. Specifically one described in . The `openpgp-card/tools` crate is set up to generate tests only if the -file `tools/virtual-card-available` exists, and the `openpgp-card` -repository `.gitlab-ci.yml` file is set up to create that file when -the repository is tested in GitLab CI. +environment variable `CARD_BASED_TESTS` is set (to any value), +and the `openpgp-card` repository `.gitlab-ci.yml` file is set up to +set that environment variable when the repository is tested in GitLab CI. This means that if you run `cargo test`, no test code is normally generated from this document. To run the tests locally, outside of