Skip to content

Commit

Permalink
Fixed: Braces processing in the default environment variable values (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored Aug 24, 2023
1 parent 1312145 commit be52919
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## v0.1.1

### Fixed

- Braces processing in the default environment variable values

## v0.1.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ src/version.h:

test: ## Run tests
@set -e; for n in $(wildcard tests/*); do \
$(MAKE) -C $$n test clean; \
$(MAKE) -C $$n test clean || exit 1; \
done

clean: ## Cleaning
Expand Down
33 changes: 30 additions & 3 deletions src/envsubst.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ char *envsubst(const char *str) {
} state = DATA, prevState = DATA;
bool flush = false;
unsigned int nested = 0;
unsigned int nestedInDef = 0;

for (unsigned int i = 0; str[i] != '\0'; i++) {
// detect the state
Expand All @@ -89,7 +90,9 @@ char *envsubst(const char *str) {
state = ENV_DEFAULT;

continue;
} else if (str[i] == '}' && (state == ENV_NAME || state == ENV_DEFAULT)) {
} else if (str[i] == '{' && nested > 0 && state == ENV_DEFAULT) {
nestedInDef++;
} else if (str[i] == '}' && nestedInDef == 0 && (state == ENV_NAME || state == ENV_DEFAULT)) {
nested--;

if (nested == 0) {
Expand All @@ -98,6 +101,8 @@ char *envsubst(const char *str) {
state = DATA;
flush = true;
}
} else if (str[i] == '}' && nested > 0 && state == ENV_DEFAULT) {
nestedInDef--;
}

const char c = str[i];
Expand Down Expand Up @@ -175,7 +180,7 @@ char *envsubst(const char *str) {
// assert(strcmp(
// envsubst(
// "__$FOO ${bar} $FOO:def ${Test_1:-def} ${Test_1} ${_UNSET_VAR_:-default} bla-bla ${FOO2:-тест}${ABC} ${}${}"),
// "__$FOO $FOO:def foo foo default bla-bla тест "
// "__$FOO $FOO:def foo foo default bla-bla тест "
// ) == 0);
//
// assert(strcmp(
Expand All @@ -188,5 +193,27 @@ char *envsubst(const char *str) {
// "aaa } "
// ) == 0);
//
// // printf("%s\n", envsubst("aaa ${}} ${${} bbb"));
// assert(strcmp(
// envsubst("__${_UNSET_VAR_:-{\"string\"}}__"),
// "__{\"string\"}__"
// ) == 0);
//
// assert(strcmp(
// envsubst("__${_UNSET_VAR_:- {} }__"),
// "__ {} __"
// ) == 0);
//
// assert(strcmp(
// envsubst("{\"a\":1, \"root\": ${_UNSET_VAR_:-{\"foo\": 123}}, \"b\":2}"),
// "{\"a\":1, \"root\": {\"foo\": 123}, \"b\":2}"
// ) == 0);
//
// putenv("__JSON_VAR__={\"foo\": \"bar\"}");
//
// printf("%s\n", envsubst("{\"a\":1, \"root\": ${__JSON_VAR__:-{}}, \"b\":2}")); // TODO: comment this line
//
// assert(strcmp(
// envsubst("{\"a\":1, \"root\": ${__JSON_VAR__:-{}}, \"b\":2}"),
// "{\"a\":1, \"root\": {\"foo\": \"bar\"}, \"b\":2}"
// ) == 0);
//}
16 changes: 16 additions & 0 deletions tests/json-obj-from-env-var/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/make

.PHONY: test clean

.ONESHELL:
test:
__JSON_LIST__='{"foo": "bar", "baz": "blah"}' ../../mustpl -f ./give.data.json ./give.template.txt 2> ./stderr.out 1> ./stdout.out
diff -u ./want.stderr.txt ./stderr.out || exit 1
diff -u ./want.stdout.txt ./stdout.out || exit 1

../../mustpl -f ./give.data.json ./give.template.txt 2> ./stderr.out 1> ./stdout.out
diff -u ./want-fallback.stderr.txt ./stderr.out || exit 1
diff -u ./want-fallback.stdout.txt ./stdout.out || exit 1

clean:
-rm -v ./*.out
4 changes: 4 additions & 0 deletions tests/json-obj-from-env-var/give.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"the_list": ${__JSON_LIST__:-{}},
"the_value": 12345
}
7 changes: 7 additions & 0 deletions tests/json-obj-from-env-var/give.template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The list:

{{#the_list.*}}
- {{ * }}: {{ . }}
{{/the_list.*}}

{{the_value}}
Empty file.
4 changes: 4 additions & 0 deletions tests/json-obj-from-env-var/want-fallback.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The list:


12345
Empty file.
6 changes: 6 additions & 0 deletions tests/json-obj-from-env-var/want.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The list:

- foo: bar
- baz: blah

12345

0 comments on commit be52919

Please sign in to comment.