From e6b5fd3de9e41c2d4cb7529d8865c1ef1c17a7a4 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Mon, 7 Feb 2022 05:57:06 +0000 Subject: [PATCH] yarn.#Build: simpler API, better tests * Remove mandatory 'cache' field * New 'name' field to customize cache IDs instead * Simplify and expand tests Signed-off-by: Solomon Hykes --- .../yarn/test/yarn-test.cue | 94 ++++++++----------- pkg/universe.dagger.io/yarn/yarn.cue | 13 ++- 2 files changed, 50 insertions(+), 57 deletions(-) diff --git a/pkg/universe.dagger.io/yarn/test/yarn-test.cue b/pkg/universe.dagger.io/yarn/test/yarn-test.cue index aaacb5ae..b33a5a3b 100644 --- a/pkg/universe.dagger.io/yarn/test/yarn-test.cue +++ b/pkg/universe.dagger.io/yarn/test/yarn-test.cue @@ -2,9 +2,9 @@ package yarn import ( "dagger.io/dagger" + "dagger.io/dagger/engine" + "universe.dagger.io/yarn" - "universe.dagger.io/alpine" - "universe.dagger.io/bash" ) dagger.#Plan & { @@ -13,60 +13,48 @@ dagger.#Plan & { testdata2: path: "./testdata2" } - actions: { - cache: dagger.#CacheDir & { - id: "yarn cache" - } + actions: tests: { - pkg: yarn.#Build & { - source: inputs.directories.testdata.contents - "cache": cache - } - - _image: alpine.#Build & { - packages: bash: {} - } - - test: bash.#Run & { - image: _image.output - mounts: build: { - dest: "/build" - contents: pkg.output + simple: { + build: yarn.#Build & { + source: inputs.directories.testdata.contents + } + + verify: #AssertFile & { + input: build.output + path: "test" + contents: "output\n" } - script: contents: #""" - test "$(cat /build/test)" = "output" - """# } - // FIXME: re-enable? - // TestSecretsAndFile: { - // pkg: #Package & { - // source: inputs.directories.testdata2 - // writeEnvFile: "/.env" - // env: { - // one: "one" - // two: "two" - // } - // secrets: { - // secretone: dagger.#Secret @dagger(input) - // secretwo: dagger.#Secret @dagger(input) - // } - // } - - // test: os.#Container & { - // image: alpine.#Image & { - // package: bash: true - // } - // shell: path: "/bin/bash" - // mount: "/build": from: pkg.build - // command: """ - // content="$(cat /build/env)" - // [[ "${content}" = *"SECRETONE="* ]] && \\ - // [[ "${content}" = *"SECRETWO="* ]] && \\ - // [[ "${content}" = *"ONE=one"* ]] && \\ - // [[ "${content}" = *"TWO=two"* ]] - // """ - // } - // } + // Run yarn.#Build with a custom name + customName: { + build: yarn.#Build & { + name: "My Build" + source: inputs.directories.testdata.contents + } + verify: #AssertFile & { + input: build.output + path: "test" + contents: "output\n" + } + } } } + +// Make an assertion on the contents of a file +#AssertFile: { + input: dagger.#FS + path: string + contents: string + + _read: engine.#ReadFile & { + "input": input + "path": path + } + + actual: _read.contents + + // Assertion + contents: actual +} diff --git a/pkg/universe.dagger.io/yarn/yarn.cue b/pkg/universe.dagger.io/yarn/yarn.cue index ae6b813b..e2019c3a 100644 --- a/pkg/universe.dagger.io/yarn/yarn.cue +++ b/pkg/universe.dagger.io/yarn/yarn.cue @@ -13,6 +13,11 @@ import ( // Build a Yarn package #Build: { + // Custom name for the build. + // When building different apps in the same plan, assign + // different names for optimal caching. + name: string | *"" + // Application source code source: dagger.#FS @@ -33,9 +38,6 @@ import ( // Fix for shadowing issues let yarnScript = script - // Cache to use, passed by the caller - cache: engine.#CacheDir - // Optional arguments for the script args: [...string] | *[] @@ -74,7 +76,10 @@ import ( mounts: { "yarn cache": { dest: "/cache/yarn" - contents: cache + contents: engine.#CacheDir & { + // FIXME: are there character limitations in cache ID? + id: "universe.dagger.io/yarn.#Build \(name)" + } } "package source": { dest: "/src"