CodeSampleX

Exemplo

tinyexec 1.3.0: NonZeroExitError

Amostra verificada para npm tinyexec 1.3.0: NonZeroExitError. O contrato rodou em node 22 · linux debian/x64 · docker e passou.

sha256:72b59841986713e9c5eff359f36e9ec3254a95ff24d73fc4c29a157e31bb0a71

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

Caso

HOW
Objetivo
verify tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0
Pacotes
Símbolos
  • tinyexec.NonZeroExitError
Ambiente
node 22.23.2
Criado
2026-09-06T14:42:53Z

Contrato

  1. NonZeroExitError is an Error subclass representing command exit failure
  2. NonZeroExitError constructor populates exitCode, signalCode, output, and non-enumerable result
  3. NonZeroExitError formats error message with command and arguments on non-zero exit
  4. NonZeroExitError formats error message with termination signal when process is killed
  5. x throws NonZeroExitError containing stdout, stderr, and exitCode when throwOnError is enabled
  6. xSync throws NonZeroExitError containing output and exitCode when throwOnError is enabled
  7. ExecProcess async iteration throws NonZeroExitError when process terminates with non-zero exit code
  8. NonZeroExitError signalCode captures process signal when process is killed with throwOnError enabled

Arquivos

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.mjs

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/tinyexec@1.3.0
Demonstrate these symbols/APIs:
  - tinyexec.NonZeroExitError

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:c4c1b270cab4700de29b180fb76b7423b339c1f5d220f6cec080021cf61a815a","contract":["NonZeroExitError is an Error subclass representing command exit failure","NonZeroExitError constructor populates exitCode, signalCode, output, and non-enumerable result","NonZeroExitError formats error message with command and arguments on non-zero exit","NonZeroExitError formats error message with termination signal when process is killed","x throws NonZeroExitError containing stdout, stderr, and exitCode when throwOnError is enabled","xSync throws NonZeroExitError containing output and exitCode when throwOnError is enabled","ExecProcess async iteration throws NonZeroExitError when process terminates with non-zero exit code","NonZeroExitError signalCode captures process signal when process is killed with throwOnError enabled"],"goal":"verify tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0","kind":"HOW","packages":["pkg:npm/tinyexec@1.3.0"],"schemaVersion":1,"symbols":["tinyexec.NonZeroExitError"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/tinyexec@1.3.0"],"schemaVersion":1,"subject":"pkg:npm/tinyexec@1.3.0","symbols":["tinyexec.NonZeroExitError"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-tinyexec-nonzeroexiterror",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-tinyexec-nonzeroexiterror",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "tinyexec": "1.3.0"
      }
    },
    "node_modules/tinyexec": {
      "version": "1.3.0",
      "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.3.0.tgz",
      "integrity": "sha512-QKAl9m8gWWGHV8jZcPeym6j+XULi6tOf1mT83WYJ4Lk2ytW/uwAWkrP0uFsdoYMdueVJ0qs26wZ+23xeB4ibNQ==",
      "license": "MIT",
      "engines": {
        "node": ">=18"
      }
    }
  }
}
package.json
{
  "name": "sample-tinyexec-nonzeroexiterror",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification of tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0",
  "type": "module",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "tinyexec": "1.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/tinyexec@1.3.0"
  ],
  "symbols": [
    "tinyexec.NonZeroExitError"
  ]
}
src/index.js
import { NonZeroExitError, x, xSync, ExecProcess } from 'tinyexec';

/**
 * Creates a NonZeroExitError instance.
 * @param {import('tinyexec').CommonOutputApi} result - Execution output API instance
 * @param {import('tinyexec').Output} [output] - Output object with stdout, stderr, exitCode
 * @param {string} [command] - Command name
 * @param {string[]} [args] - Command arguments
 * @returns {NonZeroExitError}
 */
export function createNonZeroExitError(result, output, command, args) {
  return new NonZeroExitError(result, output, command, args);
}

/**
 * Executes a command asynchronously with throwOnError enabled.
 * @param {string} command - Command to run
 * @param {string[]} [args] - Arguments
 * @param {object} [options] - Options
 * @returns {import('tinyexec').Result}
 */
export function runWithThrowOnError(command, args, options) {
  return x(command, args, { ...options, throwOnError: true });
}

/**
 * Executes a command synchronously with throwOnError enabled.
 * @param {string} command - Command to run
 * @param {string[]} [args] - Arguments
 * @param {object} [options] - Options
 * @returns {import('tinyexec').SyncResult}
 */
export function runSyncWithThrowOnError(command, args, options) {
  return xSync(command, args, { ...options, throwOnError: true });
}

export { NonZeroExitError, x, xSync, ExecProcess };
test/contract.mjs
import assert from 'node:assert/strict';
import {
  NonZeroExitError,
  x,
  xSync,
  ExecProcess,
  createNonZeroExitError,
  runWithThrowOnError,
  runSyncWithThrowOnError
} from '../src/index.js';

// 1. NonZeroExitError is an Error subclass representing command exit failure
assert.equal(typeof NonZeroExitError, 'function', 'NonZeroExitError must be a constructor function');
assert.ok(NonZeroExitError.prototype instanceof Error, 'NonZeroExitError must extend Error');
assert.equal(typeof createNonZeroExitError, 'function');
assert.equal(typeof runWithThrowOnError, 'function');
assert.equal(typeof runSyncWithThrowOnError, 'function');

// 2. NonZeroExitError constructor populates exitCode, signalCode, output, and non-enumerable result
{
  const mockResult = {
    exitCode: 42,
    signalCode: null,
    pid: 1234,
    killed: false
  };
  const mockOutput = {
    stdout: 'mock stdout',
    stderr: 'mock stderr',
    exitCode: 42
  };
  const err = createNonZeroExitError(mockResult, mockOutput, 'my-cmd', ['--flag', 'value with spaces']);
  assert.ok(err instanceof Error);
  assert.ok(err instanceof NonZeroExitError);
  assert.equal(err.exitCode, 42);
  assert.equal(err.signalCode, null);
  assert.deepEqual(err.output, mockOutput);
  assert.equal(err.result, mockResult);
  assert.equal(Object.prototype.propertyIsEnumerable.call(err, 'result'), false);
}

// 3. NonZeroExitError formats error message with command and arguments on non-zero exit
{
  const mockResult = {
    exitCode: 2,
    signalCode: null,
    pid: 2222,
    killed: false
  };
  const errWithArgs = createNonZeroExitError(mockResult, undefined, 'test-bin', ['arg1', 'arg with space']);
  assert.equal(errWithArgs.message, 'The command `test-bin arg1 "arg with space"` exited with a non-zero status (2)');

  const errNoCommand = createNonZeroExitError(mockResult);
  assert.equal(errNoCommand.message, 'The process exited with a non-zero status (2)');
}

// 4. NonZeroExitError formats error message with termination signal when process is killed
{
  const mockResult = {
    exitCode: null,
    signalCode: 'SIGKILL',
    pid: 3333,
    killed: true
  };
  const err = createNonZeroExitError(mockResult, undefined, 'long-runner');
  assert.equal(err.signalCode, 'SIGKILL');
  assert.equal(err.exitCode, 1);
  assert.equal(err.message, 'The command `long-runner` was killed by the signal SIGKILL');
}

// 5. x throws NonZeroExitError containing stdout, stderr, and exitCode when throwOnError is enabled
{
  await assert.rejects(
    async () => {
      await runWithThrowOnError('node', [
        '-e',
        'process.stdout.write("standard message\\n"); process.stderr.write("error message\\n"); process.exit(7);'
      ]);
    },
    (err) => {
      assert.ok(err instanceof NonZeroExitError, 'Expected instance of NonZeroExitError');
      assert.equal(err.exitCode, 7);
      assert.equal(err.signalCode, null);
      assert.equal(err.output?.stdout, 'standard message\n');
      assert.equal(err.output?.stderr, 'error message\n');
      assert.equal(err.output?.exitCode, 7);
      return true;
    }
  );
}

// 6. xSync throws NonZeroExitError containing output and exitCode when throwOnError is enabled
{
  assert.throws(
    () => {
      runSyncWithThrowOnError('node', [
        '-e',
        'process.stdout.write("sync stdout\\n"); process.stderr.write("sync stderr\\n"); process.exit(9);'
      ]);
    },
    (err) => {
      assert.ok(err instanceof NonZeroExitError, 'Expected instance of NonZeroExitError');
      assert.equal(err.exitCode, 9);
      assert.equal(err.signalCode, null);
      assert.equal(err.output?.stdout, 'sync stdout\n');
      assert.equal(err.output?.stderr, 'sync stderr\n');
      assert.equal(err.output?.exitCode, 9);
      return true;
    }
  );
}

// 7. ExecProcess async iteration throws NonZeroExitError when process terminates with non-zero exit code
{
  const proc = new ExecProcess(
    'node',
    ['-e', 'console.log("line-one"); console.log("line-two"); process.exit(11);'],
    { throwOnError: true }
  );
  proc.spawn();
  const receivedLines = [];
  await assert.rejects(
    async () => {
      for await (const line of proc) {
        receivedLines.push(line.trim());
      }
    },
    (err) => {
      assert.ok(err instanceof NonZeroExitError, 'Expected instance of NonZeroExitError');
      assert.equal(err.exitCode, 11);
      return true;
    }
  );
  assert.deepEqual(receivedLines, ['line-one', 'line-two']);
}

// 8. NonZeroExitError signalCode captures process signal when process is killed with throwOnError enabled
{
  await assert.rejects(
    async () => {
      const proc = runWithThrowOnError('node', ['-e', 'setInterval(() => {}, 1000);']);
      await new Promise((resolve) => setTimeout(resolve, 50));
      proc.kill('SIGTERM');
      await proc;
    },
    (err) => {
      assert.ok(err instanceof NonZeroExitError, 'Expected instance of NonZeroExitError');
      assert.equal(err.signalCode, 'SIGTERM');
      assert.ok(err.message.includes('was killed by the signal SIGTERM'));
      return true;
    }
  );
}

console.log('All contract assertions passed.');

Seeder de origem

anônimo