From fc08c9514b8a75968e48242cda259d969b2ba082 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Thu, 29 Oct 2020 12:51:21 +0000 Subject: [PATCH] Types: refactored ts_test. --- 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(-) create mode 100644 test/ts/package.json create mode 100644 test/ts/tsconfig.json diff --git a/auto/make b/auto/make index 5c910a7f..b66864eb 100644 --- a/auto/make +++ b/auto/make @@ -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 --git a/auto/sources b/auto/sources index b7c0a465..0c59df99 100644 --- a/auto/sources +++ b/auto/sources @@ -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 --git a/test/ts/package.json b/test/ts/package.json new file mode 100644 index 00000000..3daeea81 --- /dev/null +++ b/test/ts/package.json @@ -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 --git a/test/ts/test.ts b/test/ts/test.ts index dbb8df7b..ce7940de 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -1,8 +1,3 @@ -/// -/// -/// -/// - 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 --git a/test/ts/tsconfig.json b/test/ts/tsconfig.json new file mode 100644 index 00000000..f11f689a --- /dev/null +++ b/test/ts/tsconfig.json @@ -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" + ] +} -- 2.47.3