Skip to content

Commit

Permalink
introduce docker and add some unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed May 21, 2024
1 parent ce3b57a commit a0861e8
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 3 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
publish:
runs-on: ${{ matrix.os }}
needs: setup
outputs:
appname: ${{ needs.setup.outputs.appname }}
tag: ${{ needs.setup.outputs.tag }}
strategy:
matrix:
include:
Expand Down Expand Up @@ -125,3 +128,44 @@ jobs:
asset_name: ${{ matrix.asset_name }}.tar.gz
asset_content_type: application/x-gzip
upload_url: ${{ needs.setup.outputs.upload_url }}

docker:
needs: publish
runs-on: ubuntu-latest
steps:
- name: Set up QEMU
# Add support for more platforms with QEMU (optional)
# https://github.com/docker/setup-qemu-action
uses: docker/setup-qemu-action@v3

- name: Docker buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository_owner }}/${{ github.repository }}:${{ needs.publish.outputs.tag }}
tags: |
type=raw,value=latest
type=sha,prefix=,suffix=,format=short
type=raw,value=${{ needs.publish.outputs.tag }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ github.repository}}:buildcache
cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ github.repository }}:buildcache,mode=max
1 change: 1 addition & 0 deletions .github/workflows/update-version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
run: |
sed "s/^version = \".*\"/version = \"${{ steps.vars.outputs.tag }}\"/" Cargo.toml > a ; mv a Cargo.toml
sed "s/\$VERSION/${{ steps.vars.outputs.tag }}/g" templates/README.md > a ; mv a README.md
sed "s/ARG VERSION=.*/ARG VERSION=${{ steps.vars.outputs.tag }}/g" Dockerfile > a ; mv a Dockerfile
- name: Initialize Git
shell: bash
Expand Down
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM alpine:3.16 AS builder

ARG VERSION=0.1.18
ARG TARGETPLATFORM
ARG PLATFORM=${TARGETPLATFORM#linux/}

WORKDIR /home/totebag

RUN apk add --no-cache curl tar gzip \
&& curl -LO https://github.com/tamada/totebag/releases/download/v${VERSION}/totebag-${VERSION}_linux_${PLATFORM}.tar.gz \
&& tar xvfz totebag-${VERSION}_linux_${PLATFORM}.tar.gz

FROM alpine:3.16

ARG VERSION=0.1.18

LABEL org.opencontainers.image.source https://github.com/tamada/totebag

RUN apk add --no-cache libgcc musl-dev \
&& adduser -D nonroot \
&& mkdir -p /workdir

COPY --from=builder /home/totebag/totebag-${VERSION}/totebag /opt/totebag/totebag

WORKDIR /workdir
USER nonroot

ENTRYPOINT [ "/opt/totebag/totebag" ]

25 changes: 23 additions & 2 deletions src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ mod tests {
#[test]
fn test_archiver() {
let a1 = create_archiver(&PathBuf::from("results/test.tar"));
assert!(a1.is_ok());
assert_eq!(a1.unwrap().format(), Format::Tar);
if let Ok(f) = a1 {
assert_eq!(f.format(), Format::Tar);
} else {
assert!(false);
}

let a2 = create_archiver(&PathBuf::from("results/test.tar.gz"));
assert!(a2.is_ok());
Expand All @@ -148,5 +151,23 @@ mod tests {
let a5 = create_archiver(&PathBuf::from("results/test.rar"));
assert!(a5.is_ok());
assert_eq!(a5.unwrap().format(), Format::Rar);

let a6 = create_archiver(&PathBuf::from("results/test.tar.xz"));
assert!(a6.is_ok());
assert_eq!(a6.unwrap().format(), Format::TarXz);

let a7 = create_archiver(&PathBuf::from("results/test.7z"));
assert!(a7.is_ok());
assert_eq!(a7.unwrap().format(), Format::SevenZ);

let a8 = create_archiver(&PathBuf::from("results/test.unknown"));
assert!(a8.is_err());
if let Err(e) = a8 {
if let ToteError::UnknownFormat(msg) = e {
assert_eq!(msg, "test.unknown: unknown format".to_string());
} else {
assert!(false);
}
}
}
}
2 changes: 1 addition & 1 deletion src/archiver/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Archiver for TarXzArchiver {
write_tar(inout, |file| XzEncoder::new(file, 9))
}
fn format(&self) -> Format {
Format::TarBz2
Format::TarXz
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,21 @@ mod tests {
let e5 = create_extractor(&PathBuf::from("results/test.rar"));
assert!(e5.is_ok());
assert_eq!(e5.unwrap().format(), Format::Rar);

let e6 = create_extractor(&PathBuf::from("results/test.tar.xz"));
assert!(e6.is_ok());
assert_eq!(e6.unwrap().format(), Format::TarXz);

let e7 = create_extractor(&PathBuf::from("results/test.7z"));
assert!(e7.is_ok());
assert_eq!(e7.unwrap().format(), Format::SevenZ);

let e8 = create_extractor(&PathBuf::from("results/test.unknown"));
assert!(e8.is_err());
if let Err(ToteError::UnknownFormat(msg)) = e8 {
assert_eq!(msg, "test.unknown: unsupported format".to_string());
} else {
assert!(false);
}
}
}
27 changes: 27 additions & 0 deletions src/extractor/sevenz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn extract(mut file: &File, path: PathBuf, opts: &ExtractorOpts) -> Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use crate::verboser::create_verboser;

#[test]
fn test_list() {
Expand All @@ -82,4 +83,30 @@ mod tests {
Err(_) => assert!(false),
}
}

#[test]
fn test_extract_archive() {
let e = SevenZExtractor{};
let file = PathBuf::from("testdata/test.7z");
let opts = ExtractorOpts {
dest: PathBuf::from("results/sevenz"),
use_archive_name_dir: false,
overwrite: true,
v: create_verboser(false),
};
match e.perform(file, &opts) {
Ok(_) => {
assert!(true);
assert!(PathBuf::from("results/sevenz/Cargo.toml").exists());
std::fs::remove_dir_all(PathBuf::from("results/sevenz")).unwrap();
},
Err(_) => assert!(false),
};
}

#[test]
fn test_format() {
let e = SevenZExtractor{};
assert_eq!(e.format(), Format::SevenZ);
}
}
14 changes: 14 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,41 @@ mod tests {
assert!(find_format(None).is_err());
if let Ok(f) = find_format(Some(OsStr::new("hoge.zip"))) {
assert_eq!(f, Format::Zip);
assert_eq!(f.to_string(), "Zip".to_string());
}
if let Ok(f) = find_format(Some(OsStr::new("hoge.unknown"))) {
assert_eq!(f.to_string(), "hoge.unknown: unknown format".to_string());
}
if let Ok(f) = find_format(Some(OsStr::new("hoge.tar"))) {
assert_eq!(f, Format::Tar);
assert_eq!(f.to_string(), "Tar".to_string());
}
if let Ok(f) = find_format(Some(OsStr::new("hoge.rar"))) {
assert_eq!(f, Format::Rar);
assert_eq!(f.to_string(), "Rar".to_string());
}
if let Ok(f) = find_format(Some(OsStr::new("hoge.tar.gz"))) {
assert_eq!(f, Format::TarGz);
assert_eq!(f.to_string(), "TarGz".to_string());
}
if let Ok(f) = find_format(Some(OsStr::new("hoge.tar.bz2"))) {
assert_eq!(f, Format::TarBz2);
assert_eq!(f.to_string(), "TarBz2".to_string());
}
if let Ok(f) = find_format(Some(OsStr::new("hoge.tar.xz"))) {
assert_eq!(f, Format::TarXz);
assert_eq!(f.to_string(), "TarXz".to_string());
}
if let Ok(f) = find_format(Some(OsStr::new("hoge.7z"))) {
assert_eq!(f, Format::SevenZ);
assert_eq!(f.to_string(), "SevenZ".to_string());
}
if let Err(e) = find_format(None) {
if let ToteError::NoArgumentsGiven = e {
assert!(true);
} else {
assert!(false);
}
}
}
}

0 comments on commit a0861e8

Please sign in to comment.