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 036806d commit b067a2a
Show file tree
Hide file tree
Showing 8 changed files with 5,177 additions and 1 deletion.
67 changes: 67 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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: houseabsolute/actions-rust-cross@v0
with:
command: build
args: "--release"
target: ${{ matrix.platform.target }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Release
id: release
uses: ./
with:
executable-name: test-project
target: ${{ matrix.platform.target }}
- name: Install psutils on Windows
run: choco install --ignore-checksums psutils
if: runner.os == 'Windows'
- name: Check release artifacts
shell: bash
run: |
# We want to run this in a clean dir to avoid any conflicts with files in the current dir,
# like the archive file containing the release.
DIR=$(pwd)
TEMPDIR=$( mktemp -d )
cd "$TEMPDIR"
"$DIR/"tests/check-release.pl \
--artifact-id "${{ steps.release.outputs.artifact-id }}" \
--executable-name test-project \
--github-token "${{ github.token }}" \
--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.

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

# 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/main.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);
}
}
81 changes: 81 additions & 0 deletions tests/check-release.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env perl

use v5.30;
use strict;
use warnings;
no warnings 'experimental::signatures';
use feature 'signatures';

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

use autodie qw( :all );

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

sub main {
my $artifact_id;
my $executable_name;
my $github_token;
my $repo;
my $target;

GetOptions(
'artifact-id=s' => \$artifact_id,
'executable-name=s' => \$executable_name,
'github-token=s' => \$github_token,
'repo=s' => \$repo,
'target=s' => \$target,
);

system(
'curl',
'-L',
'-H', 'Accept: application/vnd.github+json',
'-H', "Authorization: Bearer $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 ? 'test-project*.zip*' : 'test-project*.tar.gz*';
my @files = glob $glob;

is( scalar @files, 2, 'found two files in the artifact tarball' )
or diag("@files");
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" )
or diag($sha256_contents);
my $shasum
= $^O eq 'MSWin32' ? 'c/programdata/chocolatey/lib/psutils/tools/shasum.ps1' : 'shasum';
my $output = capturex( $shasum, '--algorithm', '256', '--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 ( $executable_name, qw( README.md Changes.md ) ) {
ok( -f $file, "$file exists after unpacking archive" );
}

done_testing();
}

main();
Loading

0 comments on commit b067a2a

Please sign in to comment.