Skip to content

Commit 64a589d

Browse files
huanRyan Munro
authored andcommitted
add typescript tests
1 parent 02051c7 commit 64a589d

7 files changed

Lines changed: 240 additions & 6 deletions

File tree

index.d.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
Writable,
3+
Readable,
4+
} from "stream";
5+
6+
declare module "python-bridge" {
7+
interface pythonBridge extends Function {
8+
(option?: PythonBridgeOptions): PythonBridge;
9+
}
10+
export const pythonBridge: pythonBridge
11+
12+
export interface PythonBridgeOptions {
13+
intepreter: string;
14+
stdio?: [PipeStdin, PipeStdout, PipeStderr];
15+
cwd?: string;
16+
env?: { [key:string]: string; };
17+
uid?: number;
18+
gid?: number;
19+
}
20+
21+
export interface PythonBridge {
22+
(literals: TemplateStringsArray | string, ...placeholders: any[]): Promise<any>;
23+
ex(literals: TemplateStringsArray | string, ...placeholders: any[]): Promise<void>;
24+
lock<T>(withLock: (python: PythonBridge) => Promise<T>): Promise<T>
25+
pid: number;
26+
end(): Promise<void>;
27+
disconnect(): Promise<void>;
28+
kill(signal: string | number): void;
29+
stdin: Writable;
30+
stdout: Readable;
31+
stderr: Readable;
32+
connected: boolean;
33+
}
34+
35+
export function isPythonException(e: any): boolean;
36+
37+
export class PythonException extends Error {
38+
exception: {
39+
message: string;
40+
args: any[];
41+
type: { name: string; module: string; }
42+
format: string[];
43+
};
44+
traceback: {
45+
lineno: number;
46+
strack: string[];
47+
format: string[]
48+
};
49+
format: string[]
50+
}
51+
52+
export type Pipe = "pipe" | "ignore" | "inherit";
53+
export type PipeStdin = Pipe | Readable;
54+
export type PipeStdout = Pipe | Writable;
55+
export type PipeStderr = Pipe | Writable;
56+
}

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,5 @@ pythonBridge.PythonException = PythonException;
179179
pythonBridge.PythonBridgeNotConnected = PythonBridgeNotConnected;
180180
pythonBridge.isPythonException = isPythonException;
181181
pythonBridge.json = json;
182-
module.exports = pythonBridge;
182+
183+
module.exports = pythonBridge.pythonBridge = pythonBridge;

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
"version": "1.0.0",
44
"description": "Node.js to Python bridge ✨🐍🚀✨",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
7-
"test": "tap test.js"
8+
"lint": "npm run lint:ts",
9+
"lint:ts": "tslint --project tsconfig.json --type-check",
10+
"test": "npm run test:js && npm run test:ts",
11+
"test:js": "tap test.js",
12+
"test:ts": "ts-node test.ts"
813
},
914
"repository": {
1015
"type": "git",
@@ -25,7 +30,11 @@
2530
"bluebird": "^3.4.6"
2631
},
2732
"devDependencies": {
33+
"@types/node": "^8.0.14",
2834
"tap": "^2.3.4",
29-
"temp": "^0.8.3"
35+
"temp": "^0.8.3",
36+
"ts-node": "^3.2.0",
37+
"tslint": "^5.5.0",
38+
"typescript": "^2.4.1"
3039
}
3140
}

test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
'use strict';
22

3-
let pythonBridge = require('./');
4-
let PythonException = pythonBridge.PythonException;
5-
let isPythonException = pythonBridge.isPythonException;
3+
const {
4+
pythonBridge,
5+
PythonException,
6+
isPythonException,
7+
} = require('./');
8+
// let pythonBridge = require('./');
9+
// let PythonException = pythonBridge.PythonException;
10+
// let isPythonException = pythonBridge.isPythonException;
611
let test = require('tap').test;
712
let Promise = require('bluebird');
813
let mkdirTemp = Promise.promisify(require('temp').mkdir);

test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env ts-node
2+
3+
import { pythonBridge } from './index';
4+
5+
const { test } = require('tap');
6+
7+
test('TypeScript', async t => {
8+
t.plan(1);
9+
10+
let python = pythonBridge();
11+
let a = 123, b = 321;
12+
python.ex`
13+
def hello(a, b):
14+
return a + b
15+
`;
16+
const x = await python`hello(${a}, ${b})`
17+
t.equal(x, a + b)
18+
python.end();
19+
});

tsconfig.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6"
4+
, "module": "commonjs"
5+
, "outDir": "dist"
6+
, "declaration": true
7+
, "sourceMap": true
8+
, "noEmitOnError": true
9+
, "noUnusedLocals": true
10+
, "noImplicitReturns": true
11+
, "noFallthroughCasesInSwitch": true
12+
, "strictNullChecks": true
13+
, "noImplicitAny": false
14+
, "noUnusedParameters": false
15+
, "noImplicitThis": true
16+
17+
, "noLib": false
18+
, "traceResolution": false
19+
}
20+
, "exclude": [
21+
"node_modules/"
22+
, "dist/"
23+
]
24+
, "include": [
25+
"test.ts"
26+
]
27+
}

tslint.json

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{
2+
"rules": {
3+
"align": [
4+
true,
5+
"parameters",
6+
// "arguments",
7+
"statements"
8+
],
9+
"jsdoc-require": [
10+
false
11+
],
12+
"ban": false,
13+
"class-name": true,
14+
"comment-format": [
15+
true,
16+
"check-space"
17+
],
18+
"curly": false,
19+
"eofline": true,
20+
"forin": false,
21+
"indent": [
22+
true,
23+
"spaces"
24+
],
25+
"interface-name": [false],
26+
"jsdoc-format": true,
27+
"label-position": true,
28+
"max-line-length": [
29+
true,
30+
180
31+
],
32+
"callable-types": true,
33+
"import-blacklist": [true, "rxjs"],
34+
"interface-over-type-literal": true,
35+
"no-empty-interface": true,
36+
"no-string-throw": true,
37+
"prefer-const": true,
38+
"typeof-compare": true,
39+
"unified-signatures": false,
40+
"no-inferrable-types": [true, "ignore-params"],
41+
"member-access": true,
42+
"member-ordering": [false],
43+
"no-any": false,
44+
"no-arg": true,
45+
"no-bitwise": true,
46+
"no-conditional-assignment": true,
47+
"no-consecutive-blank-lines": [
48+
true
49+
],
50+
"no-console": [false],
51+
"no-construct": false,
52+
"no-debugger": true,
53+
"no-duplicate-variable": true,
54+
"no-empty": true,
55+
"no-eval": true,
56+
"no-internal-module": true,
57+
"no-require-imports": false,
58+
"no-shadowed-variable": true,
59+
"no-string-literal": false,
60+
"no-switch-case-fall-through": true,
61+
"no-trailing-whitespace": true,
62+
"no-unused-expression": true,
63+
// "no-unused-variable": true,
64+
"no-use-before-declare": true,
65+
"no-var-keyword": true,
66+
"no-var-requires": false,
67+
"object-literal-sort-keys": false,
68+
"one-line": [
69+
true,
70+
"check-open-brace",
71+
"check-whitespace"
72+
],
73+
"quotemark": [
74+
true,
75+
"single",
76+
"avoid-escape"
77+
],
78+
"radix": false,
79+
"semicolon": [
80+
false
81+
],
82+
"switch-default": false,
83+
"trailing-comma": [
84+
true,
85+
{
86+
"multiline": "always",
87+
"singleline": "never"
88+
}
89+
],
90+
"triple-equals": [true],
91+
"typedef": [false],
92+
"typedef-whitespace": [
93+
true,
94+
{
95+
"call-signature": "nospace",
96+
"index-signature": "nospace",
97+
"parameter": "nospace",
98+
"property-declaration": "nospace",
99+
"variable-declaration": "nospace"
100+
}
101+
],
102+
"variable-name": [
103+
true,
104+
"check-format",
105+
"allow-leading-underscore",
106+
"ban-keywords"
107+
],
108+
"whitespace": [
109+
true,
110+
"check-branch",
111+
"check-decl",
112+
"check-operator",
113+
"check-separator",
114+
"check-type"
115+
]
116+
}
117+
}

0 commit comments

Comments
 (0)