Skip to content

chore(pg-connection-string): use tsx for tests #3443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/pg-connection-string/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand All @@ -23,4 +24,7 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules
package-lock.json
package-lock.json

# TypeScript output directory
dist
4 changes: 4 additions & 0 deletions packages/pg-connection-string/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extension": ["js", "ts"],
"require": "tsx"
}
3 changes: 0 additions & 3 deletions packages/pg-connection-string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ pg-connection-string

[![NPM](https://nodei.co/npm/pg-connection-string.png?compact=true)](https://nodei.co/npm/pg-connection-string/)

[![Build Status](https://travis-ci.org/iceddev/pg-connection-string.svg?branch=master)](https://travis-ci.org/iceddev/pg-connection-string)
[![Coverage Status](https://coveralls.io/repos/github/iceddev/pg-connection-string/badge.svg?branch=master)](https://coveralls.io/github/iceddev/pg-connection-string?branch=master)

Functions for dealing with a PostgresSQL connection string

`parse` method taken from [node-postgres](https://github.com/brianc/node-postgres.git)
Expand Down
23 changes: 21 additions & 2 deletions packages/pg-connection-string/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ClientConfig } from 'pg'
//import { ClientConfig } from 'pg'
import { ClientConfig as PgClientConfig } from 'pg'

export function parse(connectionString: string, options?: Options): ConnectionOptions

Expand All @@ -7,18 +8,36 @@ export interface Options {
useLibpqCompat?: boolean
}

interface SSLConfig {
ca?: string
cert?: string | null
key?: string
rejectUnauthorized?: boolean
}

export interface ConnectionOptions {
host: string | null
password?: string
user?: string
port?: string | null
database: string | null | undefined
client_encoding?: string
ssl?: boolean | string
ssl?: boolean | string | SSLConfig

application_name?: string
fallback_application_name?: string
options?: string
keepalives?: number

// We allow any other options to be passed through
[key: string]: unknown
}

// FIXME: remove this when https://github.com/DefinitelyTyped/DefinitelyTyped/pull/72576 lands
export interface ClientConfig extends PgClientConfig {
fallback_application_name?: string
client_encoding?: string
options?: string
}

export function toClientConfig(config: ConnectionOptions): ClientConfig
Expand Down
8 changes: 2 additions & 6 deletions packages/pg-connection-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,8 @@ function toClientConfig(config) {
if (typeof sslConfig === 'boolean') {
c[key] = sslConfig
}
// else path is taken. multiple tests produce a sslConfig that is an object
// and we can console.log to see that we take this path
//
// see https://github.com/istanbuljs/babel-plugin-istanbul/issues/186#issuecomment-1137765139
// istanbul ignore else
else if (typeof sslConfig === 'object') {

if (typeof sslConfig === 'object') {
c[key] = toConnectionOptions(sslConfig)
}
} else if (value !== undefined && value !== null) {
Expand Down
12 changes: 6 additions & 6 deletions packages/pg-connection-string/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
}
},
"scripts": {
"test": "istanbul cover _mocha && npm run check-coverage",
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --lines 100 --functions 100",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
"test": "nyc --reporter=lcov mocha && npm run check-coverage",
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --lines 100 --functions 100"
},
"repository": {
"type": "git",
Expand All @@ -36,9 +35,10 @@
"homepage": "https://github.com/brianc/node-postgres/tree/master/packages/pg-connection-string",
"devDependencies": {
"chai": "^4.1.1",
"coveralls": "^3.0.4",
"istanbul": "^0.4.5",
"mocha": "^10.5.2"
"mocha": "^10.5.2",
"nyc": "^15",
"tsx": "^4.19.3",
"typescript": "^4.0.3"
},
"files": [
"index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use strict'

const chai = require('chai')
import chai from 'chai'
const expect = chai.expect
chai.should()

const { parse, toClientConfig, parseIntoClientConfig } = require('../')
import { parse, toClientConfig, parseIntoClientConfig } from '../'

describe('toClientConfig', function () {
it('converts connection info', function () {
const config = parse('postgres://brian:pw@boom:381/lala')
const clientConfig = toClientConfig(config)

clientConfig.user.should.equal('brian')
clientConfig.password.should.equal('pw')
clientConfig.host.should.equal('boom')
clientConfig.port.should.equal(381)
clientConfig.database.should.equal('lala')
clientConfig.user?.should.equal('brian')
clientConfig.password?.should.equal('pw')
clientConfig.host?.should.equal('boom')
clientConfig.port?.should.equal(381)
clientConfig.database?.should.equal('lala')
})

it('converts query params', function () {
Expand All @@ -24,45 +22,47 @@ describe('toClientConfig', function () {
)
const clientConfig = toClientConfig(config)

clientConfig.application_name.should.equal('TheApp')
clientConfig.fallback_application_name.should.equal('TheAppFallback')
clientConfig.client_encoding.should.equal('utf8')
clientConfig.options.should.equal('-c geqo=off')
clientConfig.application_name?.should.equal('TheApp')
clientConfig.fallback_application_name?.should.equal('TheAppFallback')
clientConfig.client_encoding?.should.equal('utf8')
clientConfig.options?.should.equal('-c geqo=off')
})

it('converts SSL boolean', function () {
const config = parse('pg:///?ssl=true')
const clientConfig = toClientConfig(config)

clientConfig.ssl.should.equal(true)
clientConfig.ssl?.should.equal(true)
})

it('converts sslmode=disable', function () {
const config = parse('pg:///?sslmode=disable')
const clientConfig = toClientConfig(config)

clientConfig.ssl.should.equal(false)
clientConfig.ssl?.should.equal(false)
})

it('converts sslmode=noverify', function () {
const config = parse('pg:///?sslmode=no-verify')
const clientConfig = toClientConfig(config)

clientConfig.ssl.rejectUnauthorized.should.equal(false)
clientConfig.ssl?.should.deep.equal({
rejectUnauthorized: false,
})
})

it('converts other sslmode options', function () {
const config = parse('pg:///?sslmode=verify-ca')
const clientConfig = toClientConfig(config)

clientConfig.ssl.should.deep.equal({})
clientConfig.ssl?.should.deep.equal({})
})

it('converts other sslmode options', function () {
const config = parse('pg:///?sslmode=verify-ca')
const clientConfig = toClientConfig(config)

clientConfig.ssl.should.deep.equal({})
clientConfig.ssl?.should.deep.equal({})
})

it('converts ssl cert options', function () {
Expand All @@ -77,7 +77,7 @@ describe('toClientConfig', function () {
const config = parse(connectionString)
const clientConfig = toClientConfig(config)

clientConfig.ssl.should.deep.equal({
clientConfig.ssl?.should.deep.equal({
ca: 'example ca\n',
cert: 'example cert\n',
key: 'example key\n',
Expand All @@ -87,9 +87,9 @@ describe('toClientConfig', function () {
it('converts unix domain sockets', function () {
const config = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus')
const clientConfig = toClientConfig(config)
clientConfig.host.should.equal('/some path/')
clientConfig.database.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"')
clientConfig.client_encoding.should.equal('utf8')
clientConfig.host?.should.equal('/some path/')
clientConfig.database?.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"')
clientConfig.client_encoding?.should.equal('utf8')
})

it('handles invalid port', function () {
Expand All @@ -106,20 +106,20 @@ describe('toClientConfig', function () {

const clientConfig = toClientConfig(config)

clientConfig.host.should.equal('boom')
clientConfig.database.should.equal('lala')
clientConfig.ssl.should.deep.equal({})
clientConfig.host?.should.equal('boom')
clientConfig.database?.should.equal('lala')
clientConfig.ssl?.should.deep.equal({})
})
})

describe('parseIntoClientConfig', function () {
it('converts url', function () {
const clientConfig = parseIntoClientConfig('postgres://brian:pw@boom:381/lala')

clientConfig.user.should.equal('brian')
clientConfig.password.should.equal('pw')
clientConfig.host.should.equal('boom')
clientConfig.port.should.equal(381)
clientConfig.database.should.equal('lala')
clientConfig.user?.should.equal('brian')
clientConfig.password?.should.equal('pw')
clientConfig.host?.should.equal('boom')
clientConfig.port?.should.equal(381)
clientConfig.database?.should.equal('lala')
})
})
Loading