Skip to content

Commit

Permalink
bun testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Hilgers committed May 29, 2024
1 parent 58153c2 commit 5fa17a7
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 42 deletions.
1 change: 1 addition & 0 deletions qrcloak/qrcloak-bindings/languages/js/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ npm_package(
":qrcloak-bindings-wasm-bundler",
":qrcloak-bindings-wasm-nodejs",
],
package = "@fhilgers/qrcloak",
)

exports_files(
Expand Down
21 changes: 3 additions & 18 deletions qrcloak/qrcloak-bindings/languages/js/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")
load(":bun.bzl", "bun_test")

sh_binary(
name = "script",
srcs = [
"script.sh",
],
)

sh_test(
bun_test(
name = "qrcloak-js-test",
srcs = [
"script.sh",
],
data = [
"lib.test.ts",
"//qrcloak/qrcloak-bindings/languages/js",
"@bun//:bin",
],
env = {
"BUN": "$(location @bun//:bin)",
"TESTFILE": "$(location lib.test.ts)",
"QRCLOAK": "$(location //qrcloak/qrcloak-bindings/languages/js:js)",
},
entry_point = "lib.test.ts",
)
70 changes: 70 additions & 0 deletions qrcloak/qrcloak-bindings/languages/js/test/bun.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
load("@aspect_rules_js//npm:providers.bzl", "NpmPackageInfo")
load("@aspect_bazel_lib//lib:copy_file.bzl", "COPY_FILE_TOOLCHAINS", "copy_file_action")

def _bun_test_impl(ctx):
runfiles = ctx.runfiles(files = ctx.files.data + ctx.files._runfiles)

outs = []
for data in ctx.attr.data:
if NpmPackageInfo in data:
info = data[NpmPackageInfo]
package = info.package
directory = info.directory
out = ctx.actions.declare_directory(ctx.label.name + "/node_modules/" + package)
ctx.actions.symlink(output = out, target_file = directory)
outs.append(out)

entry = ctx.actions.declare_file(ctx.label.name + "/" + ctx.attr.entry_point[DefaultInfo].files.to_list()[0].basename)
copy_file_action(ctx, ctx.attr.entry_point[DefaultInfo].files.to_list()[0], entry)

bun = ctx.attr._bun[DefaultInfo].files_to_run.executable
outs.append(entry)
outs.append(bun)

node_modules = ctx.runfiles(files = outs)

runfiles = runfiles.merge(node_modules)

entry_path = ctx.workspace_name + "/" + entry.short_path
bun_path = ctx.workspace_name + "/" + bun.short_path

out = ctx.actions.declare_file(ctx.label.name + ".sh")
ctx.actions.write(
output = out,
content =
"""\
#!/usr/bin/env bash
# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---
DIR="$(dirname $(rlocation "%s"))"
BUN="$(rlocation %s)"
cd $DIR && $BUN test
""" % (entry_path, bun_path),
is_executable = True,
)

return [
DefaultInfo(executable = out, runfiles = runfiles),
]

bun_test = rule(
implementation = _bun_test_impl,
attrs = {
"entry_point": attr.label(mandatory = True, allow_files = [".ts", ".js"]),
"data": attr.label_list(providers = [NpmPackageInfo]),
"_bun": attr.label(default = "@bun//:bin", allow_files = True),
"_runfiles": attr.label(default = "@bazel_tools//tools/bash/runfiles"),
},
test = True,
toolchains = COPY_FILE_TOOLCHAINS,
)
1 change: 0 additions & 1 deletion qrcloak/qrcloak-bindings/languages/js/test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
PayloadGenerator,
PayloadMerger,
PayloadSplitter,
//} from "../../../bazel-out/k8-fastbuild/bin/qrcloak-word/node_modules/.aspect_rules_js/@[email protected]/node_modules/@fhilgers/qrcloak";
} from "@fhilgers/qrcloak";

import { expect, test } from "bun:test";
Expand Down
11 changes: 0 additions & 11 deletions qrcloak/qrcloak-bindings/languages/js/test/script.sh

This file was deleted.

5 changes: 3 additions & 2 deletions tools/unzip_jars.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from typing import List
import zipfile
import argparse


def unzip_files(jar_files, output_directory):
def unzip_files(jar_files: List[str], output_directory: str):
if not os.path.exists(output_directory):
os.makedirs(output_directory)

Expand All @@ -12,7 +13,7 @@ def unzip_files(jar_files, output_directory):
zip_ref.extractall(output_directory)


def rchmod(dir):
def rchmod(dir: str):
os.chmod(dir, 0o755)

for root, dirs, files in os.walk(dir):
Expand Down
14 changes: 7 additions & 7 deletions tools/workflow_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import zipfile
from tqdm import tqdm
from textwrap import dedent
from typing import NoReturn

custom_bar_format = "[{bar:39}] {percentage:3.0f}% {desc}"

Expand All @@ -16,34 +17,33 @@ def install_jre():
)


def check_sha256(filename, expected_sha256):
def check_sha256(filename: str, expected_sha256: str):
sha256_hash = hashlib.sha256()
with open(filename, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest() == expected_sha256


class DownloadProgressBar(tqdm):
def update_to(self, b=1, bsize=1, tsize=None):
if tsize is not None:
self.total = tsize
class DownloadProgressBar(tqdm[NoReturn]):
def update_to(self, b: int, bsize: int, tsize: int):
self.total = tsize

previous = self.n
current = min(b * bsize, tsize)

self.update(current - previous)


def download_with_progress(url, filename):
def download_with_progress(url: str, filename: str):
with DownloadProgressBar(
desc=filename, bar_format=custom_bar_format, ascii=" ="
) as t:
urllib.request.urlretrieve(url, filename, reporthook=t.update_to)
t.close()


def unzip_with_progress(zip_file, extract_to):
def unzip_with_progress(zip_file: str, extract_to: str):
with zipfile.ZipFile(zip_file, "r") as zip_ref:
total_files = len(zip_ref.infolist())
with tqdm(
Expand Down
7 changes: 4 additions & 3 deletions tools/zip.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import os
import re
from typing import List
import zipfile
import argparse


def should_exclude(file_path, excludes, base_directory):
def should_exclude(file_path: str, excludes: List[str], base_directory: str):
relative_path = os.path.relpath(file_path, base_directory)
for pattern in excludes:
if re.match(pattern, relative_path):
return True
return False


def zip_directory(directory, zip_filename, excludes):
def zip_directory(directory: str, zip_filename: str, excludes: List[str]):
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as jar_file:
for root, dirs, files in os.walk(directory):
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, start=directory)
Expand Down

0 comments on commit 5fa17a7

Please sign in to comment.