diff --git a/{{cookiecutter.project_slug}}/merge_production_dotenvs_in_dotenv.py b/{{cookiecutter.project_slug}}/merge_production_dotenvs_in_dotenv.py new file mode 100644 index 000000000..48c02eb39 --- /dev/null +++ b/{{cookiecutter.project_slug}}/merge_production_dotenvs_in_dotenv.py @@ -0,0 +1,65 @@ +import os +import pytest + +PROJECT_DIR_PATH = os.path.dirname(os.path.realpath(__file__)) +PRODUCTION_DOTENV_DIR_PATH = os.path.join(PROJECT_DIR_PATH, '.envs', '.production') +PRODUCTION_DOTENV_FILE_PATHS = [ + os.path.join(PRODUCTION_DOTENV_DIR_PATH, '.django'), + os.path.join(PRODUCTION_DOTENV_DIR_PATH, '.postgres'), + os.path.join(PRODUCTION_DOTENV_DIR_PATH, '.caddy'), +] +DOTENV_FILE_PATH = os.path.join(PROJECT_DIR_PATH, '.env') + + +def merge(output_file_path, merged_file_paths, append_linesep=True): + with open(output_file_path, 'w+') as output_file: + for merged_file_path in merged_file_paths: + with open(merged_file_path, 'r') as merged_file: + merged_file_content = merged_file.read() + output_file.write(merged_file_content) + if append_linesep: + output_file.write(os.linesep) + + +def main(): + merge(DOTENV_FILE_PATH, PRODUCTION_DOTENV_FILE_PATHS) + + +@pytest.mark.parametrize('merged_file_count', range(3)) +@pytest.mark.parametrize('append_linesep', [True, False]) +def test_when_merging_given_valid_arguments_should_succeed(tmpdir_factory, + merged_file_count, + append_linesep): + tmp_dir_path = str(tmpdir_factory.getbasetemp()) + + output_file_path = os.path.join(tmp_dir_path, '.env') + + expected_output_file_content = '' + merged_file_paths = [] + for i in range(merged_file_count): + merged_file_ord = i + 1 + + merged_filename = '.service{}'.format(merged_file_ord) + merged_file_path = os.path.join(tmp_dir_path, merged_filename) + + merged_file_content = merged_filename * merged_file_ord + + with open(merged_file_path, 'w+') as file: + file.write(merged_file_content) + + expected_output_file_content += merged_file_content + if append_linesep: + expected_output_file_content += os.linesep + + merged_file_paths.append(merged_file_path) + + merge(output_file_path, merged_file_paths, append_linesep) + + with open(output_file_path, 'r') as output_file: + actual_output_file_content = output_file.read() + + assert actual_output_file_content == expected_output_file_content + + +if __name__ == '__main__': + main()