Skip to content

Instantly share code, notes, and snippets.

View tzafrirben's full-sized avatar

Tzafrir Ben Ami tzafrirben

View GitHub Profile
@tzafrirben
tzafrirben / test-child-process-with-di.ts
Last active December 26, 2022 13:38
Test child process with dependency injection
describe('Test execute commands in a spawn child process', () => {
test('execCommand should return command stdout/stderr', async () => {
// create a "fake" child process
const process = mockChildProcess();
// note that execCommand returns a promise, so this fake child process was
// not spawned yet
const cmdExec = execCommand(process);
// since we control the fake child process, we can emit the data events on
@tzafrirben
tzafrirben / dependency-injection.ts
Last active December 26, 2022 11:56
Dependency injection
import { ChildProcess } from 'child_process';
export const execCommand = async (childProcess: ChildProcess) => {
return new Promise((resolve, reject) => {
let stdout = '';
childProcess.stdout?.on('data', data => {
stdout = stdout + data.toString();
});
let stderr = '';
@tzafrirben
tzafrirben / test-cmd.ts
Last active December 26, 2022 13:26
Testing child process with Jest mock
jest.mock('child_process');
describe('Test execute commands in a spawn child process', () => {
test('execCommand should return command stdout/stderr', async () => {
// create a "fake" child process
const process = mockChildProcess();
// return the "fake" child process object when calling child_process.spawn
jest.spyOn(child_process, 'spawn').mockReturnValueOnce(process);
@tzafrirben
tzafrirben / fake-child-process.ts
Last active December 28, 2022 08:22
Create a fake child process
import * as child_process from 'child_process';
import EventEmitter from 'events';
import { Readable } from 'stream';
const mockChildProcess = (): child_process.ChildProcess => {
const proc = new EventEmitter() as child_process.ChildProcess;
proc.stdout = new EventEmitter() as Readable;
proc.stderr = new EventEmitter() as Readable;
@tzafrirben
tzafrirben / cmd.ts
Last active December 26, 2022 13:24
Spawn child process
import { spawn } from 'child_process';
export const execCommand = async (
command: string,
workDir: string
): Promise<{ stdout: string; stderr: string; code: number }> => {
return new Promise((resolve, reject) => {
const childProcess = spawn(command, {
shell: true,
cwd: workDir
name: Tweet water level
on:
schedule:
- cron: '30 14 * * *'
jobs:
tweet:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
name: Water level history
on:
schedule:
- cron: '00 14 * * *'
jobs:
update-history:
runs-on: ubuntu-latest
name: update water level
steps:
- name: Checkout
[;;; query-params & form-params
parameters/parameters-middleware
;;; content-negotiation
muuntaja/format-negotiate-middleware
;;; encoding response body
muuntaja/format-response-middleware
;;; coerce response body to keys to camelCase
[middleware/letter-case-response {:to :camelCase}]
;;; exception handling
exception/exception-middleware
(defn letter-case-request
[handler & [{:as options}]]
(fn [{:keys [body-params query-params] :as request}]
(let [from-letter-case (or (:from options) (:from default-options))
to-letter-case (or (:to options) (:to default-options))]
(handler
(cond-> request
(coll? body-params) (assoc :body-params
(transform-keys-letter-case
(filter-letter-case-keys body-params from-letter-case)
@tzafrirben
tzafrirben / letter-case-response.clj
Last active September 1, 2020 12:11
convert request letter case
(defn letter-case-response
[handler & [{:as options}]]
(let [to-letter-case (or (:to options) (:to default-options))]
(fn [request]
(let [response (handler request)]
(if (coll? (:body response))
(update response :body #(transform-keys-letter-case
% to-letter-case))
response)))))