From 4b76b26c87d0140709798e6a4683f62f18f5867c Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Tue, 9 Nov 2021 20:10:00 -0600 Subject: [PATCH 01/22] .example.env command fixed issue with windows directories removed debug echo statements Removed old try accept that is no longer needed Should be the end of my after the fact website commits Add missing trailing newline when adding new value Sometimes, the source file doesn't have a trailing newline. If we add a new binding in such a case, we need to add a newline before the new binding. Release version 0.19.2 updated to remove file error with broken file fixed minor bug tests and minor changes --- CHANGELOG.md | 10 +++++++++- setup.cfg | 2 +- src/dotenv/cli.py | 29 +++++++++++++++++++++++++++-- src/dotenv/main.py | 4 ++++ src/dotenv/version.py | 2 +- tests/test_cli.py | 6 ++++++ tests/test_main.py | 1 + 7 files changed, 49 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b2b2bbb..9b18856e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.19.2] - 2021-11-11 + +### Fixed + +- In `set_key`, add missing newline character before new entry if necessary. (#361 by + [@bbc2]) + ## [0.19.1] - 2021-08-09 ### Added @@ -292,7 +299,8 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). [@yannham]: https://github.com/yannham [@zueve]: https://github.com/zueve -[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v0.19.1...HEAD +[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v0.19.2...HEAD +[0.19.2]: https://github.com/theskumar/python-dotenv/compare/v0.19.1...v0.19.2 [0.19.1]: https://github.com/theskumar/python-dotenv/compare/v0.19.0...v0.19.1 [0.19.0]: https://github.com/theskumar/python-dotenv/compare/v0.18.0...v0.19.0 [0.18.0]: https://github.com/theskumar/python-dotenv/compare/v0.17.1...v0.18.0 diff --git a/setup.cfg b/setup.cfg index b63622d6..d87b0a6b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.19.1 +current_version = 0.19.2 commit = True tag = True diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index b7ae24af..9e13db9d 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -82,7 +82,6 @@ def get(ctx: click.Context, key: Any) -> None: else: exit(1) - @cli.command() @click.pass_context @click.argument('key', required=True) @@ -125,6 +124,32 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None: ret = run_command(commandline, dotenv_as_dict) exit(ret) +@cli.command() +@click.pass_context +def example_file(ctx: click.Context) -> None: + '''Generates a .example.env file without values.''' + filedir = ctx.obj['FILE'].replace("\\", "/") + if not os.path.isfile(filedir): + raise click.BadParameter( + 'Path "%s" does not exist.' % (filedir), + ctx=ctx + ) + newFileList = [] + with open(filedir,'r') as file: + for line in file: + line = line.strip() + if line[0] != "#": + line = line.split("=", 1)[0] + "=" + newFileList.append(line + "\n") + + while newFileList[-1] == "\n": + newFileList.pop(-1) + + newFileName = f"{os.path.basename(file.name).split('.', 1)[0]}.example.env" + with open(newFileName, "w") as newFile: + for line in newFileList: + newFile.write(line) + click.echo(f"{newFileName} exported.") def run_command(command: List[str], env: Dict[str, str]) -> int: """Run command in sub process. @@ -161,4 +186,4 @@ def run_command(command: List[str], env: Dict[str, str]) -> int: if __name__ == "__main__": - cli() + cli() \ No newline at end of file diff --git a/src/dotenv/main.py b/src/dotenv/main.py index b8d0a4e0..d867f023 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -167,13 +167,17 @@ def set_key( with rewrite(dotenv_path) as (source, dest): replaced = False + missing_newline = False for mapping in with_warn_for_invalid_lines(parse_stream(source)): if mapping.key == key_to_set: dest.write(line_out) replaced = True else: dest.write(mapping.original.string) + missing_newline = not mapping.original.string.endswith("\n") if not replaced: + if missing_newline: + dest.write("\n") dest.write(line_out) return True, key_to_set, value_to_set diff --git a/src/dotenv/version.py b/src/dotenv/version.py index 4c1ca3c8..aa070c2c 100644 --- a/src/dotenv/version.py +++ b/src/dotenv/version.py @@ -1 +1 @@ -__version__ = "0.19.1" +__version__ = "0.19.2" diff --git a/tests/test_cli.py b/tests/test_cli.py index 223476fe..b0299307 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -205,3 +205,9 @@ def test_run_with_version(cli): assert result.exit_code == 0 assert result.output.strip().endswith(__version__) + +def test_example_file_non_existent_file(cli): + result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'example_file']) + + assert result.exit_code == 2, result.output + assert "does not exist" in result.output \ No newline at end of file diff --git a/tests/test_main.py b/tests/test_main.py index 13e2791c..541ac5ee 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -37,6 +37,7 @@ def test_set_key_no_file(tmp_path): ("a=b\nc=d", "a", "e", (True, "a", "e"), "a='e'\nc=d"), ("a=b\nc=d\ne=f", "c", "g", (True, "c", "g"), "a=b\nc='g'\ne=f"), ("a=b\n", "c", "d", (True, "c", "d"), "a=b\nc='d'\n"), + ("a=b", "c", "d", (True, "c", "d"), "a=b\nc='d'\n"), ], ) def test_set_key(dotenv_file, before, key, value, expected, after): From b5cb2e3ed5f247ecc6cf12597de092fa4197a7d9 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 30 May 2022 22:34:38 -0500 Subject: [PATCH 02/22] implemented suggestions --- src/dotenv/cli.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 9e13db9d..5cfe963d 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -126,7 +126,7 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None: @cli.command() @click.pass_context -def example_file(ctx: click.Context) -> None: +def generate_sample(ctx: click.Context) -> None: '''Generates a .example.env file without values.''' filedir = ctx.obj['FILE'].replace("\\", "/") if not os.path.isfile(filedir): @@ -134,22 +134,19 @@ def example_file(ctx: click.Context) -> None: 'Path "%s" does not exist.' % (filedir), ctx=ctx ) - newFileList = [] + newEnvList = [] with open(filedir,'r') as file: for line in file: line = line.strip() if line[0] != "#": line = line.split("=", 1)[0] + "=" - newFileList.append(line + "\n") + newEnvList.append(line + "\n") - while newFileList[-1] == "\n": - newFileList.pop(-1) + while newEnvList[-1] == "\n": + newEnvList.pop(-1) - newFileName = f"{os.path.basename(file.name).split('.', 1)[0]}.example.env" - with open(newFileName, "w") as newFile: - for line in newFileList: - newFile.write(line) - click.echo(f"{newFileName} exported.") + for line in newEnvList: + click.echo(line) def run_command(command: List[str], env: Dict[str, str]) -> int: """Run command in sub process. @@ -186,4 +183,4 @@ def run_command(command: List[str], env: Dict[str, str]) -> int: if __name__ == "__main__": - cli() \ No newline at end of file + cli() From f844040932e0b28510a59eed193bd9c07ae3fd1b Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 30 May 2022 22:44:35 -0500 Subject: [PATCH 03/22] Update test_cli.py --- tests/test_cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index b0299307..be40bd5b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -206,8 +206,8 @@ def test_run_with_version(cli): assert result.exit_code == 0 assert result.output.strip().endswith(__version__) -def test_example_file_non_existent_file(cli): - result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'example_file']) +def test_generate_sample_non_existent_file(cli): + result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'generate_sample']) assert result.exit_code == 2, result.output - assert "does not exist" in result.output \ No newline at end of file + assert "does not exist" in result.output From cde7a580e7fef3242fe13f72f7ae41879c2e844a Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Sun, 5 Jun 2022 22:05:26 -0500 Subject: [PATCH 04/22] Added tests for comment preservation and value removal --- tests/test_cli.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index be40bd5b..05e52124 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -209,5 +209,22 @@ def test_run_with_version(cli): def test_generate_sample_non_existent_file(cli): result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'generate_sample']) - assert result.exit_code == 2, result.output + assert result.exit_code == 2 assert "does not exist" in result.output + +def test_generate_sample_comment_preservation(cli, dotenv_file): + sample = "# a = b\n#c = d\n # e=f\n #g = h" # tests different spacings of comments + with open(dotenv_file, "w") as f: + f.write(sample) + result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate_sample']) + + assert result.exit_code == 0 + assert result.output == sample + +def test_generate_sample_value_removal(cli, dotenv_file): + with open(dotenv_file, "w") as f: + f.write("a=b") + result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate_sample']) + + assert result.exit_code == 0, result.output + assert 'b' not in result.output From afb206e0c8afd6f3b6710eef1df76144f32028ab Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Sun, 5 Jun 2022 22:09:12 -0500 Subject: [PATCH 05/22] fixing spacing --- tests/test_cli.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 05e52124..bfd5b455 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -206,12 +206,14 @@ def test_run_with_version(cli): assert result.exit_code == 0 assert result.output.strip().endswith(__version__) + def test_generate_sample_non_existent_file(cli): result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'generate_sample']) assert result.exit_code == 2 assert "does not exist" in result.output + def test_generate_sample_comment_preservation(cli, dotenv_file): sample = "# a = b\n#c = d\n # e=f\n #g = h" # tests different spacings of comments with open(dotenv_file, "w") as f: @@ -221,6 +223,7 @@ def test_generate_sample_comment_preservation(cli, dotenv_file): assert result.exit_code == 0 assert result.output == sample + def test_generate_sample_value_removal(cli, dotenv_file): with open(dotenv_file, "w") as f: f.write("a=b") From e0d0f9157bb24117a99513484c359190b8071860 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Sun, 5 Jun 2022 22:19:54 -0500 Subject: [PATCH 06/22] might have fixed test error --- src/dotenv/cli.py | 2 ++ tests/test_cli.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index c4a54a48..60472ee9 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -124,6 +124,7 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None: ret = run_command(commandline, dotenv_as_dict) exit(ret) + @cli.command() @click.pass_context def generate_sample(ctx: click.Context) -> None: @@ -148,6 +149,7 @@ def generate_sample(ctx: click.Context) -> None: for line in newEnvList: click.echo(line) + def run_command(command: List[str], env: Dict[str, str]) -> int: """Run command in sub process. diff --git a/tests/test_cli.py b/tests/test_cli.py index bfd5b455..db63651f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -221,7 +221,7 @@ def test_generate_sample_comment_preservation(cli, dotenv_file): result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate_sample']) assert result.exit_code == 0 - assert result.output == sample + assert result.output == f'{sample}\n' def test_generate_sample_value_removal(cli, dotenv_file): From eb2c82b017b7c49bf71cad5aba730b58de9b1028 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Sun, 5 Jun 2022 22:25:17 -0500 Subject: [PATCH 07/22] python 3.5 doesn't support f-strings --- tests/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index db63651f..97c8f49b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -221,7 +221,7 @@ def test_generate_sample_comment_preservation(cli, dotenv_file): result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate_sample']) assert result.exit_code == 0 - assert result.output == f'{sample}\n' + assert result.output == (sample + "\n") def test_generate_sample_value_removal(cli, dotenv_file): From f96ddc72311a1995f5c9032444a0872c4f8b63d7 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Sun, 5 Jun 2022 22:19:54 -0500 Subject: [PATCH 08/22] tests still not working --- src/dotenv/cli.py | 2 ++ tests/test_cli.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index c4a54a48..60472ee9 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -124,6 +124,7 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None: ret = run_command(commandline, dotenv_as_dict) exit(ret) + @cli.command() @click.pass_context def generate_sample(ctx: click.Context) -> None: @@ -148,6 +149,7 @@ def generate_sample(ctx: click.Context) -> None: for line in newEnvList: click.echo(line) + def run_command(command: List[str], env: Dict[str, str]) -> int: """Run command in sub process. diff --git a/tests/test_cli.py b/tests/test_cli.py index bfd5b455..97c8f49b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -221,7 +221,7 @@ def test_generate_sample_comment_preservation(cli, dotenv_file): result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate_sample']) assert result.exit_code == 0 - assert result.output == sample + assert result.output == (sample + "\n") def test_generate_sample_value_removal(cli, dotenv_file): From a9d8776f004c5309ea2a997671e88ccd45edbbf2 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:08:21 -0500 Subject: [PATCH 09/22] Fixing test syntax --- tests/test_cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 97c8f49b..6a108be7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -208,7 +208,7 @@ def test_run_with_version(cli): def test_generate_sample_non_existent_file(cli): - result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'generate_sample']) + result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'generate-sample']) assert result.exit_code == 2 assert "does not exist" in result.output @@ -218,7 +218,7 @@ def test_generate_sample_comment_preservation(cli, dotenv_file): sample = "# a = b\n#c = d\n # e=f\n #g = h" # tests different spacings of comments with open(dotenv_file, "w") as f: f.write(sample) - result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate_sample']) + result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample']) assert result.exit_code == 0 assert result.output == (sample + "\n") @@ -227,7 +227,7 @@ def test_generate_sample_comment_preservation(cli, dotenv_file): def test_generate_sample_value_removal(cli, dotenv_file): with open(dotenv_file, "w") as f: f.write("a=b") - result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate_sample']) + result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample']) assert result.exit_code == 0, result.output assert 'b' not in result.output From 4ef6e12ca4aa36176bbcdb435eb8d7030fd0fd95 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:12:39 -0500 Subject: [PATCH 10/22] Fixed how test handled newlines Text file needs to end with a newline --- tests/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 6a108be7..80c93072 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -215,7 +215,7 @@ def test_generate_sample_non_existent_file(cli): def test_generate_sample_comment_preservation(cli, dotenv_file): - sample = "# a = b\n#c = d\n # e=f\n #g = h" # tests different spacings of comments + sample = "# a = b\n#c = d\n # e=f\n #g = h\n" # tests different spacings of comments with open(dotenv_file, "w") as f: f.write(sample) result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample']) From 1e4928b6eefaeead39d7bf6448d8db592af88677 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:18:10 -0500 Subject: [PATCH 11/22] Clean up duplicate whitespace in generated sample --- src/dotenv/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 60472ee9..861a8a28 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -138,7 +138,7 @@ def generate_sample(ctx: click.Context) -> None: newEnvList = [] with open(filedir,'r') as file: for line in file: - line = line.strip() + line = " ".join(line.strip().split()) if line[0] != "#": line = line.split("=", 1)[0] + "=" newEnvList.append(line + "\n") From 33492dc329ec29bc5efb150e5b832315da060109 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:21:08 -0500 Subject: [PATCH 12/22] Fixed test to work with cleanup of duplicate whitespace --- tests/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 80c93072..f82d2277 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -221,7 +221,7 @@ def test_generate_sample_comment_preservation(cli, dotenv_file): result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample']) assert result.exit_code == 0 - assert result.output == (sample + "\n") + assert result.output == "# a = b\n# c = d\n# e=f\n# g = h\n\n" def test_generate_sample_value_removal(cli, dotenv_file): From 4c6ce11b6ba790dba20228b5dff48babdd0de60f Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:25:24 -0500 Subject: [PATCH 13/22] New whitespace issues --- src/dotenv/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 861a8a28..88587245 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -141,7 +141,7 @@ def generate_sample(ctx: click.Context) -> None: line = " ".join(line.strip().split()) if line[0] != "#": line = line.split("=", 1)[0] + "=" - newEnvList.append(line + "\n") + newEnvList.append(line) while newEnvList[-1] == "\n": newEnvList.pop(-1) From 897ce24a988637ed8ec1678ac57ba43f838d3ba9 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:30:26 -0500 Subject: [PATCH 14/22] finalized behavior of whitespace spacing between hashtags is cleaned up, 1 trailing newline at the end of the file. --- src/dotenv/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 88587245..663a2431 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -142,7 +142,7 @@ def generate_sample(ctx: click.Context) -> None: if line[0] != "#": line = line.split("=", 1)[0] + "=" newEnvList.append(line) - + newEnvList[-1] += "\n" while newEnvList[-1] == "\n": newEnvList.pop(-1) From d68fad5a18d7880f1dc29d709c68a9524393b714 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:34:17 -0500 Subject: [PATCH 15/22] added more cleanup to make behavior more consistent equals sign cleanup added --- src/dotenv/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 663a2431..12de0636 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -138,7 +138,7 @@ def generate_sample(ctx: click.Context) -> None: newEnvList = [] with open(filedir,'r') as file: for line in file: - line = " ".join(line.strip().split()) + line = line.replace(" ", "").replace("#", "# ").replace("=", "= ") if line[0] != "#": line = line.split("=", 1)[0] + "=" newEnvList.append(line) From 6062be65e4e245736ba94582bd29cc3b9cd9f44c Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:36:14 -0500 Subject: [PATCH 16/22] forgot that equals sign needed another space --- src/dotenv/cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 12de0636..e9996488 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -138,11 +138,12 @@ def generate_sample(ctx: click.Context) -> None: newEnvList = [] with open(filedir,'r') as file: for line in file: - line = line.replace(" ", "").replace("#", "# ").replace("=", "= ") + line = line.replace(" ", "").replace("#", "# ").replace("=", " = ") if line[0] != "#": line = line.split("=", 1)[0] + "=" newEnvList.append(line) newEnvList[-1] += "\n" + while newEnvList[-1] == "\n": newEnvList.pop(-1) From 1f834aa5c48196f858ef62faf509cb31c3ab6402 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:36:47 -0500 Subject: [PATCH 17/22] updated tests for the final time --- tests/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index f82d2277..e8e8d4cc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -221,7 +221,7 @@ def test_generate_sample_comment_preservation(cli, dotenv_file): result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample']) assert result.exit_code == 0 - assert result.output == "# a = b\n# c = d\n# e=f\n# g = h\n\n" + assert result.output == "# a = b\n# c = d\n# e = f\n# g = h\n\n" def test_generate_sample_value_removal(cli, dotenv_file): From ebe5a8b2866b84ae664b6736a18647b2296d62ee Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:39:41 -0500 Subject: [PATCH 18/22] Update cli.py --- src/dotenv/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index e9996488..b0e3056b 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -141,7 +141,7 @@ def generate_sample(ctx: click.Context) -> None: line = line.replace(" ", "").replace("#", "# ").replace("=", " = ") if line[0] != "#": line = line.split("=", 1)[0] + "=" - newEnvList.append(line) + newEnvList.append(line[:-1]) # [:-1] removes trailing newline newEnvList[-1] += "\n" while newEnvList[-1] == "\n": From 415a0518f785f8c59bc4d47007f61491de45a83b Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:46:57 -0500 Subject: [PATCH 19/22] preserves equal sign spacing in a comment --- src/dotenv/cli.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index b0e3056b..577ddc42 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -138,10 +138,12 @@ def generate_sample(ctx: click.Context) -> None: newEnvList = [] with open(filedir,'r') as file: for line in file: - line = line.replace(" ", "").replace("#", "# ").replace("=", " = ") + line = line.replace(" ", "") if line[0] != "#": - line = line.split("=", 1)[0] + "=" - newEnvList.append(line[:-1]) # [:-1] removes trailing newline + line = line.replace("=", " = ").split("=", 1)[0] + "=" + else: + line = "# " + line[1:] # inserts space between comment and '#' + newEnvList.append(line[:-1]) # slicing removes trailing newline newEnvList[-1] += "\n" while newEnvList[-1] == "\n": From f71f1608c757a927fb2a283cb3437700f4c77530 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 16:50:15 -0500 Subject: [PATCH 20/22] modified test to be accurate and add testing of equals sign cleanup --- tests/test_cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index e8e8d4cc..18cf8766 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -214,14 +214,14 @@ def test_generate_sample_non_existent_file(cli): assert "does not exist" in result.output -def test_generate_sample_comment_preservation(cli, dotenv_file): - sample = "# a = b\n#c = d\n # e=f\n #g = h\n" # tests different spacings of comments +def test_generate_sample_cleanup_and_comment_preservation(cli, dotenv_file): + sample = "# a = b\n#c = d\n # e=f\n #g = h\ni=j" # tests different spacings of comments with open(dotenv_file, "w") as f: f.write(sample) result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample']) assert result.exit_code == 0 - assert result.output == "# a = b\n# c = d\n# e = f\n# g = h\n\n" + assert result.output == "# a = b\n# c = d\n# e=f\n# g = h\ni = j\n\n" def test_generate_sample_value_removal(cli, dotenv_file): From 3b307fbe742e239430b11aadb6960eedc691c018 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 17:15:46 -0500 Subject: [PATCH 21/22] fixed comment preservation and it's test --- src/dotenv/cli.py | 8 ++++---- tests/test_cli.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 577ddc42..d550415e 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -138,12 +138,12 @@ def generate_sample(ctx: click.Context) -> None: newEnvList = [] with open(filedir,'r') as file: for line in file: - line = line.replace(" ", "") + line = line.strip() if line[0] != "#": - line = line.replace("=", " = ").split("=", 1)[0] + "=" + line = line.replace(" ", "").replace("=", " = ").split("=", 1)[0] + "=" else: - line = "# " + line[1:] # inserts space between comment and '#' - newEnvList.append(line[:-1]) # slicing removes trailing newline + line = "# " + line[1:].strip() # inserts space between comment and '#' + newEnvList.append(line) # slicing removes trailing newline newEnvList[-1] += "\n" while newEnvList[-1] == "\n": diff --git a/tests/test_cli.py b/tests/test_cli.py index 18cf8766..f15547a1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -221,7 +221,7 @@ def test_generate_sample_cleanup_and_comment_preservation(cli, dotenv_file): result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample']) assert result.exit_code == 0 - assert result.output == "# a = b\n# c = d\n# e=f\n# g = h\ni = j\n\n" + assert result.output == "# a = b\n# c = d\n# e=f\n# g = h\ni =\n\n" def test_generate_sample_value_removal(cli, dotenv_file): From 706d18167968ac6d17df9d915cc8f918f466e982 Mon Sep 17 00:00:00 2001 From: ProfessorPiggos Date: Mon, 6 Jun 2022 17:21:45 -0500 Subject: [PATCH 22/22] fixed what are presumably linting errors --- src/dotenv/cli.py | 8 ++++---- tests/test_cli.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index d550415e..24aebe89 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -136,16 +136,16 @@ def generate_sample(ctx: click.Context) -> None: ctx=ctx ) newEnvList = [] - with open(filedir,'r') as file: + with open(filedir, 'r') as file: for line in file: line = line.strip() if line[0] != "#": line = line.replace(" ", "").replace("=", " = ").split("=", 1)[0] + "=" else: - line = "# " + line[1:].strip() # inserts space between comment and '#' - newEnvList.append(line) # slicing removes trailing newline + line = "# " + line[1:].strip() # inserts space between comment and '#' + newEnvList.append(line) # slicing removes trailing newline newEnvList[-1] += "\n" - + while newEnvList[-1] == "\n": newEnvList.pop(-1) diff --git a/tests/test_cli.py b/tests/test_cli.py index f15547a1..9343085e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -215,9 +215,9 @@ def test_generate_sample_non_existent_file(cli): def test_generate_sample_cleanup_and_comment_preservation(cli, dotenv_file): - sample = "# a = b\n#c = d\n # e=f\n #g = h\ni=j" # tests different spacings of comments + sample = "# a = b\n#c = d\n # e=f\n #g = h\ni=j" with open(dotenv_file, "w") as f: - f.write(sample) + f.write(sample) result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample']) assert result.exit_code == 0