Skip to content

Commit

Permalink
Add tests for this action
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed Sep 8, 2024
1 parent 8b91bb1 commit ba47696
Show file tree
Hide file tree
Showing 9 changed files with 5,183 additions and 1 deletion.
57 changes: 57 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Self-test

on:
push:
pull_request:

jobs:
test:
name: Test
strategy:
fail-fast: false
matrix:
platform:
- platform_name: Linux-x86_64
os: ubuntu-20.04
target: x86_64-unknown-linux-musl
cache-cross-binary: true
- platform_name: Linux-powerpc64
os: ubuntu-20.04
target: powerpc64-unknown-linux-gnu
cache-cross-binary: true
- platform_name: Windows-x86_64
os: windows-latest
target: x86_64-pc-windows-msvc
- platform_name: macOS-aarch64
os: macOS-latest
target: aarch64-apple-darwin

runs-on: ${{ matrix.platform.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Copy test project to root
shell: bash
run: |
cp -a test-project/* .
rm -fr test-project
- name: Build binary
uses: actions-rust-cross@v0
with:
command: build
target: ${{ matrix.platform.target }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Release
id: release
uses: .
with:
binary-name: test-project
target: ${{ matrix.platform.target }}
- name: Check release artifacts
shell: bash
run: |
tests/check-release.pl \
--artifact-id ${{ steps.release.outputs.artifact-id }}" \
--binary-name test-project \
--repo houseabsolute/actions-rust-release \
--target "${{ matrix.platform.target }}"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/package.json
.\#*
\#*\#

test-project/**/target/**
7 changes: 7 additions & 0 deletions test-project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions test-project/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "test-project"
version = "0.1.0"
edition = "2021"

# For testing it would be nice to create a binary with spaces in the name, but
# right now the `name` value must be a valid crate name, and there's no
# separate setting for the compiled executable's name. See
# https://github.com/rust-lang/cargo/issues/9778.
[[bin]]
name = "bin1"
path = "src/bin1.rs"

[[bin]]
name = "bin2"
path = "src/bin2.rs"

# workaround for https://github.com/cross-rs/cross/issues/1345
[package.metadata.cross.target.x86_64-unknown-netbsd]
pre-build = [
"mkdir -p /tmp/netbsd",
"curl https://cdn.netbsd.org/pub/NetBSD/NetBSD-9.2/amd64/binary/sets/base.tar.xz -O",
"tar -C /tmp/netbsd -xJf base.tar.xz",
"cp /tmp/netbsd/usr/lib/libexecinfo.so /usr/local/x86_64-unknown-netbsd/lib",
"rm base.tar.xz",
"rm -rf /tmp/netbsd",
]
11 changes: 11 additions & 0 deletions test-project/src/bin1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
println!("Hello, world!");
}

#[cfg(test)]
mod test {
#[test]
fn test_something() {
assert_eq!(1, 1);
}
}
11 changes: 11 additions & 0 deletions test-project/src/bin2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
println!("Hello, world!");
}

#[cfg(test)]
mod test {
#[test]
fn test_something() {
assert_eq!(1, 1);
}
}
74 changes: 74 additions & 0 deletions tests/check-release.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env perl

use v5.30;
use strict;
use warnings;
no warnings 'experimental::signatures';
use feature 'signatures';
use autodie qw( :all );

use FindBin qw( $Bin );
use File::Spec;
use lib File::Spec->catdir( $Bin, 'lib' );

use Getopt::Long;
use IPC::System::Simple qw( capturex );
use Test::More;

sub main {
my $artifact_id;
my $binary_name;
my $repo;
my $target;

GetOptions(
'artifact-id=s' => \$artifact_id,
'binary-name=s' => \$binary_name,
'repo=s' => \$repo,
'target=s' => \$target,
);

system(
'curl',
'-L',
'-H', 'Accept: application/vnd.github+json',
'-H', "Authorization: Bearer $ENV{GITHUB_TOKEN}",
'-o', 'artifact.zip',
"https://api.github.com/repos/$repo/actions/artifacts/$artifact_id/zip",
);

system( 'unzip', 'artifact.zip' );

my $glob = $target =~ /windows/i ? '*.zip*' : '*.tar.gz*';
my @files = glob $glob;

is( scalar @files, 2, 'found two files in the artifact tarball' );
my ($archive_file) = grep { !/sha256/ } @files;
my ($checksum_file) = grep {/sha256/} @files;

ok( $archive_file, 'found an archive file in the artifact tarball' );
ok( $checksum_file, 'found a checksum file in the artifact tarball' );

open my $fh, '<', $checksum_file;
my $sha256_contents = do { local $/; <$fh> };
$sha256_contents =~ s/^\s+|\s+$//g;
my ( $checksum, $filename ) = $sha256_contents =~ /^(\S+) (\S+)$/;
is( $filename, $archive_file, "filename in checksum file matches archive filename" );
my $output = capturex( 'sha256sum', '--check', $checksum_file );
like( $output, qr/\Q$archive_file\E: OK/, 'sha256sum reports checksum is OK' );

if ( $archive_file =~ /\.zip$/ ) {
system( 'unzip', $archive_file );
}
else {
system( 'tar', 'xzf', $archive_file );
}

for my $file ( $binary_name, qw( README.md Changes.md ) ) {
ok( -f $file, "$file exists after unpacking archive" );
}

done_testing();
}

main();
Loading

0 comments on commit ba47696

Please sign in to comment.