From 07cdfb2e4ec761021477271776b81f33af0e731d Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 17 Mar 2021 17:56:37 +0100 Subject: towards wallet-core / util split --- packages/taler-util/src/talerconfig.test.ts | 124 ++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 packages/taler-util/src/talerconfig.test.ts (limited to 'packages/taler-util/src/talerconfig.test.ts') diff --git a/packages/taler-util/src/talerconfig.test.ts b/packages/taler-util/src/talerconfig.test.ts new file mode 100644 index 000000000..2f920fccf --- /dev/null +++ b/packages/taler-util/src/talerconfig.test.ts @@ -0,0 +1,124 @@ +/* + This file is part of GNU Taler + (C) 2020 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +/** + * Imports + */ +import test from "ava"; +import { pathsub, Configuration } from "./talerconfig"; + +test("pathsub", (t) => { + t.assert("foo" === pathsub("foo", () => undefined)); + + t.assert("fo${bla}o" === pathsub("fo${bla}o", () => undefined)); + + const d: Record = { + w: "world", + f: "foo", + "1foo": "x", + foo_bar: "quux", + }; + + t.is( + pathsub("hello ${w}!", (v) => d[v]), + "hello world!", + ); + + t.is( + pathsub("hello ${w} ${w}!", (v) => d[v]), + "hello world world!", + ); + + t.is( + pathsub("hello ${x:-blabla}!", (v) => d[v]), + "hello blabla!", + ); + + // No braces + t.is( + pathsub("hello $w!", (v) => d[v]), + "hello world!", + ); + t.is( + pathsub("hello $foo!", (v) => d[v]), + "hello $foo!", + ); + t.is( + pathsub("hello $1foo!", (v) => d[v]), + "hello $1foo!", + ); + t.is( + pathsub("hello $$ world!", (v) => d[v]), + "hello $$ world!", + ); + t.is( + pathsub("hello $$ world!", (v) => d[v]), + "hello $$ world!", + ); + + t.is( + pathsub("hello $foo_bar!", (v) => d[v]), + "hello quux!", + ); + + // Recursive lookup in default + t.is( + pathsub("hello ${x:-${w}}!", (v) => d[v]), + "hello world!", + ); + + // No variables in variable name part + t.is( + pathsub("hello ${${w}:-x}!", (v) => d[v]), + "hello ${${w}:-x}!", + ); + + // Missing closing brace + t.is( + pathsub("hello ${w!", (v) => d[v]), + "hello ${w!", + ); +}); + +test("path expansion", (t) => { + const config = new Configuration(); + config.setString("paths", "taler_home", "foo/bar"); + config.setString( + "paths", + "taler_data_home", + "$TALER_HOME/.local/share/taler/", + ); + config.setString( + "exchange", + "master_priv_file", + "${TALER_DATA_HOME}/exchange/offline-keys/master.priv", + ); + t.is( + config.getPath("exchange", "MaStER_priv_file").required(), + "foo/bar/.local/share/taler//exchange/offline-keys/master.priv", + ); +}); + +test("recursive path resolution", (t) => { + console.log("recursive test"); + const config = new Configuration(); + config.setString("paths", "a", "x${b}"); + config.setString("paths", "b", "y${a}"); + config.setString("foo", "x", "z${a}"); + t.throws(() => { + config.getPath("foo", "a").required(); + }); +}); -- cgit v1.2.3