This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ''; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Tweet water level | |
on: | |
schedule: | |
- cron: '30 14 * * *' | |
jobs: | |
tweet: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Water level history | |
on: | |
schedule: | |
- cron: '00 14 * * *' | |
jobs: | |
update-history: | |
runs-on: ubuntu-latest | |
name: update water level | |
steps: | |
- name: Checkout |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[;;; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))) |
NewerOlder