[njs] Types: refactored ts_test.

Dmitry Volyntsev xeioex at nginx.com
Thu Oct 29 13:00:15 UTC 2020


details:   https://hg.nginx.org/njs/rev/fa3ffb3a159e
branches:  
changeset: 1555:fa3ffb3a159e
user:      Jakub Jirutka <jakub at jirutka.cz>
date:      Thu Oct 29 12:51:21 2020 +0000
description:
Types: refactored ts_test.

diffstat:

 auto/make             |  19 +++++++++++++++++--
 auto/sources          |   2 ++
 test/ts/package.json  |  15 +++++++++++++++
 test/ts/test.ts       |  22 +++++++++-------------
 test/ts/tsconfig.json |  35 +++++++++++++++++++++++++++++++++++
 5 files changed, 78 insertions(+), 15 deletions(-)

diffs (172 lines):

diff -r a3ae0d653f49 -r fa3ffb3a159e auto/make
--- a/auto/make	Thu Oct 29 12:50:05 2020 +0000
+++ b/auto/make	Thu Oct 29 12:51:21 2020 +0000
@@ -248,6 +248,9 @@ END
 njs_ts_deps=`echo $NJS_TS_SRCS \
         | sed -e "s# *\([^ ][^ ]*\)#\1$njs_regex_cont#g"`
 
+njs_test_ts_deps=`echo $NJS_TEST_TS_SRCS \
+        | sed -e "s# *\([^ ][^ ]*\)#\1$njs_regex_cont#g"`
+
 cat << END >> $NJS_MAKEFILE
 
 $NJS_BUILD_DIR/ts/package.json: $njs_ts_deps
@@ -270,8 +273,20 @@ ts: $NJS_BUILD_DIR/ts/package.json
 ts_lint: $NJS_BUILD_DIR/ts/node_modules
 	cd $NJS_BUILD_DIR/ts && \$(NPM) run lint
 
-ts_test: ts
-	tsc ./test/ts/test.ts
+$NJS_BUILD_DIR/test/ts/package.json: $njs_test_ts_deps
+	mkdir -p $NJS_BUILD_DIR/test
+	cp -fr test/ts $NJS_BUILD_DIR/test/
+
+$NJS_BUILD_DIR/test/ts/node_modules: \\
+	$NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz \\
+	$NJS_BUILD_DIR/test/ts/package.json
+	cd $NJS_BUILD_DIR/test/ts && \$(NPM) install \\
+		--save-dev file:../../njs-types-\$(NJS_TYPES_VER).tgz
+	cd $NJS_BUILD_DIR/test/ts && \$(NPM) install
+	touch $NJS_BUILD_DIR/test/ts/node_modules
+
+ts_test: $NJS_BUILD_DIR/test/ts/node_modules
+	cd $NJS_BUILD_DIR/test/ts && \$(NPM) test
 
 ts_publish: ts_clean $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz
 	cd $NJS_BUILD_DIR/ && \$(NPM) publish njs-types-\$(NJS_TYPES_VER).tgz
diff -r a3ae0d653f49 -r fa3ffb3a159e auto/sources
--- a/auto/sources	Thu Oct 29 12:50:05 2020 +0000
+++ b/auto/sources	Thu Oct 29 12:51:21 2020 +0000
@@ -73,3 +73,5 @@ NJS_TEST_SRCS=" \
 "
 
 NJS_TS_SRCS=$(find ts/ -name "*.d.ts" -o -name "*.json")
+
+NJS_TEST_TS_SRCS=$(find test/ts/ -name "*.ts" -o -name "*.json")
diff -r a3ae0d653f49 -r fa3ffb3a159e test/ts/package.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/ts/package.json	Thu Oct 29 12:51:21 2020 +0000
@@ -0,0 +1,15 @@
+{
+  "private": true,
+  "name": "njs-types-test",
+  "version": "0.0.0",
+  "description": "Tests for njs TypeScript type definitions.",
+  "scripts": {
+    "test": "tsc"
+  },
+  "author": "NGINX, Inc.",
+  "license": "BSD-2-Clause",
+  "devDependencies": {
+    "njs-types": "file:../../ts",
+    "typescript": "~4.0.3"
+  }
+}
diff -r a3ae0d653f49 -r fa3ffb3a159e test/ts/test.ts
--- a/test/ts/test.ts	Thu Oct 29 12:50:05 2020 +0000
+++ b/test/ts/test.ts	Thu Oct 29 12:51:21 2020 +0000
@@ -1,8 +1,3 @@
-/// <reference path="../../build/ts/ngx_http_js_module.d.ts" />
-/// <reference path="../../build/ts/fs.d.ts" />
-/// <reference path="../../build/ts/querystring.d.ts" />
-/// <reference path="../../build/ts/crypto.d.ts" />
-
 import fs from 'fs';
 import qs from 'querystring';
 import crypto from 'crypto';
@@ -15,9 +10,9 @@ function http_module(r: NginxHTTPRequest
 
     s = 'ordinary string';
     bs = String.bytesFrom('000000', 'hex');
-    bs = s.toBytes();
+    var bs2: NjsByteString | null = s.toBytes();
     bs = s.toUTF8();
-    bs.fromBytes(null, null);
+    bs.fromBytes(undefined, undefined);
 
     s = bs + '';
 
@@ -30,29 +25,30 @@ function http_module(r: NginxHTTPRequest
 
     bs = r.args.x;
     bs = r.args[1];
-    s = r.args.x.fromUTF8();
+    var s2: string | null = r.args.x.fromUTF8();
     s = r.args.x + '';
 
     // r.headersIn
 
-    r.headersIn['Accept'].fromBytes() == 'dddd';
+    r.headersIn['Accept']?.fromBytes() == 'dddd';
 
     // r.headersOut
 
     r.headersOut['Content-Type'] = 'text/plain';
     // Warning: r.headersOut['Content-Type'] = ['a', 'b'];
     r.headersOut['Connection'] = undefined;
-    r.headersOut['Connection'] = null;
+
+    delete r.headersOut['Bar'];
 
     r.headersOut['Set-Cookie'] = ['aaa', 'bbb'];
     r.headersOut['Foo'] = ['aaa', 'bbb'];
 
-    r.subrequest('/uri', reply => r.return(200, reply.headersOut["Location"]));
+    r.subrequest('/uri', reply => r.return(200, reply.headersOut["Location"] ?? ''));
 
     // r.log
 
     r.log(bs);
-    r.log(r.headersOut['Connection']);
+    r.log(r.headersOut['Connection'] ?? '');
 
     // r.variables
 
@@ -64,7 +60,7 @@ function http_module(r: NginxHTTPRequest
     r.subrequest('/p/sub2', reply => r.return(reply.status));
     r.subrequest('/p/sub3', {detached:true});
     r.subrequest('/p/sub4', 'a=1&b=2').then(reply => r.return(reply.status,
-                                        JSON.stringify(JSON.parse(reply.responseBody))));
+                                        JSON.stringify(JSON.parse(reply.responseBody ?? ''))));
 
 }
 
diff -r a3ae0d653f49 -r fa3ffb3a159e test/ts/tsconfig.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/ts/tsconfig.json	Thu Oct 29 12:51:21 2020 +0000
@@ -0,0 +1,35 @@
+{
+    "compilerOptions": {
+        "target": "ES5",
+        "module": "es2015",
+        "lib": [
+            "ES2015",
+            "ES2016.Array.Include",
+            "ES2017.Object",
+            "ES2017.String"
+        ],
+        "noEmit": true,
+        "downlevelIteration": true,
+
+        "strict": true,
+        "noImplicitAny": true,
+        "strictNullChecks": true,
+        "strictFunctionTypes": true,
+        "strictBindCallApply": true,
+        "strictPropertyInitialization": true,
+        "noImplicitThis": true,
+        "alwaysStrict": true,
+
+        "moduleResolution": "node",
+        "forceConsistentCasingInFileNames": true
+    },
+    "include": [
+        "**/*.ts"
+    ],
+    "exclude": [
+        "./node_modules/**"
+    ],
+    "files": [
+        "./node_modules/njs-types/ngx_http_js_module.d.ts"
+    ]
+}


More information about the nginx-devel mailing list