Skip to content

Commit eefd4d1

Browse files
authored
Merge pull request #44 from rsgalloway/issue43/stray_include
Version 0.8.1
2 parents 8f6812d + 61eddf2 commit eefd4d1

7 files changed

Lines changed: 330 additions & 22 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ tmp/
1515
venv*/
1616
.vscode/
1717
keys.env
18-
encrypted.env
18+
encrypted.env
19+
baketest.env

lib/envstack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
"""
3535

3636
__prog__ = "envstack"
37-
__version__ = "0.8.0"
37+
__version__ = "0.8.1"
3838

3939
from envstack.env import clear, init, revert, save # noqa: F401

lib/envstack/util.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def get_stacks():
413413
return sorted(list(stacks))
414414

415415

416-
def findenv(var_name):
416+
def findenv(var_name: str):
417417
"""
418418
Returns a list of paths where the given environment var is set.
419419
@@ -552,7 +552,7 @@ def dump_yaml(file_path: str, data: dict, unquote: bool = True):
552552
pass
553553

554554

555-
def partition_platform_data(data):
555+
def partition_platform_data(data: dict):
556556
"""
557557
Given a data dictionary with keys 'all', 'darwin', 'linux', 'windows',
558558
this function finds which key-value pairs are common across all platforms,
@@ -562,13 +562,11 @@ def partition_platform_data(data):
562562
:param data: dictionary to partition.
563563
:returns: platform partitioned dictionary.
564564
"""
565-
566-
# move data under the "all" key if it's not already there
565+
# ensure all is present
567566
if "all" not in data:
568-
data["all"] = data.copy()
567+
data["all"] = {} # data.copy()
569568

570569
# platforms of interest (darwin, linux, windows)
571-
# platforms = [k for k in data.keys() if k not in ("all", "include")]
572570
platforms = ["darwin", "linux", "windows"]
573571

574572
# get the union of keys from all platforms
@@ -581,10 +579,8 @@ def partition_platform_data(data):
581579
for key in all_platform_keys:
582580
if all(key in data[p] for p in platforms):
583581
# get first value for comparison later
584-
# first_value = data[platforms[0]][key].value
585582
first_value = data[platforms[0]][key]
586583
# call it common if all platforms have the same value
587-
# if all(data[p][key].value == first_value for p in platforms):
588584
if all(data[p][key] == first_value for p in platforms):
589585
common_keys.append(key)
590586

@@ -614,8 +610,10 @@ def partition_platform_data(data):
614610
for p in platforms:
615611
new_data[p] = new_platform_dicts[p]
616612

617-
# add include if it exists
613+
# ensure include is present
618614
if data.get("include"):
619615
new_data["include"] = data["include"]
616+
else:
617+
new_data["include"] = []
620618

621619
return new_data

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
setup(
4242
name="envstack",
43-
version="0.8.0",
43+
version="0.8.1",
4444
description="Stacked environment variable management system",
4545
long_description=long_description,
4646
long_description_content_type="text/markdown",

tests/test_cmds.py

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,222 @@ def test_thing(self):
350350
self.assertEqual(output, expected_output)
351351

352352

353+
class TestBake(unittest.TestCase):
354+
"""Tests bake command."""
355+
356+
def setUp(self):
357+
self.filename = "baketest.env"
358+
if os.path.exists(self.filename):
359+
os.remove(self.filename)
360+
self.envstack_bin = os.path.join(
361+
os.path.dirname(__file__), "..", "bin", "envstack"
362+
)
363+
envpath = os.path.join(os.path.dirname(__file__), "..", "env")
364+
self.root = {
365+
"linux": "/mnt/pipe",
366+
"win32": "X:/pipe",
367+
"darwin": "/Volumes/pipe",
368+
}.get(sys.platform)
369+
os.environ["ENVPATH"] = envpath
370+
os.environ["INTERACTIVE"] = "0"
371+
# remove so we use base64 encoding by default
372+
if AESGCMEncryptor.KEY_VAR_NAME in os.environ:
373+
del os.environ[AESGCMEncryptor.KEY_VAR_NAME]
374+
if FernetEncryptor.KEY_VAR_NAME in os.environ:
375+
del os.environ[FernetEncryptor.KEY_VAR_NAME]
376+
377+
def tearDown(self):
378+
if os.path.exists(self.filename):
379+
os.remove(self.filename)
380+
381+
def test_default(self):
382+
"""Tests baking the default stack."""
383+
command = f"%s -o {self.filename}; cat {self.filename}" % self.envstack_bin
384+
expected_output = """#!/usr/bin/env envstack
385+
include: []
386+
all: &all
387+
<<: *all
388+
DEPLOY_ROOT: ${ROOT}/${ENV}
389+
ENV: prod
390+
ENVPATH: ${DEPLOY_ROOT}/env:${ENVPATH}
391+
HELLO: ${HELLO:=world}
392+
LOG_LEVEL: ${LOG_LEVEL:=INFO}
393+
PATH: ${DEPLOY_ROOT}/bin:${PATH}
394+
PYTHONPATH: ${DEPLOY_ROOT}/lib/python:${PYTHONPATH}
395+
darwin:
396+
<<: *all
397+
ROOT: /Volumes/pipe
398+
linux:
399+
<<: *all
400+
ROOT: /mnt/pipe
401+
windows:
402+
<<: *all
403+
ROOT: X:/pipe
404+
"""
405+
output = subprocess.check_output(
406+
command,
407+
shell=True,
408+
universal_newlines=True,
409+
)
410+
self.assertEqual(output, expected_output)
411+
412+
def test_dev(self):
413+
"""Tests baking the dev stack."""
414+
command = f"%s dev -o {self.filename}; cat {self.filename}" % self.envstack_bin
415+
expected_output = """#!/usr/bin/env envstack
416+
include: []
417+
all: &all
418+
<<: *all
419+
DEPLOY_ROOT: ${ROOT}/dev
420+
ENV: dev
421+
ENVPATH: ${ROOT}/dev/env:${ROOT}/prod/env:${ENVPATH}
422+
HELLO: ${HELLO:=world}
423+
LOG_LEVEL: DEBUG
424+
PATH: ${ROOT}/dev/bin:${ROOT}/prod/bin:${PATH}
425+
PYTHONPATH: ${ROOT}/dev/lib/python:${ROOT}/prod/lib/python:${PYTHONPATH}
426+
darwin:
427+
<<: *all
428+
ROOT: /Volumes/pipe
429+
linux:
430+
<<: *all
431+
ROOT: /mnt/pipe
432+
windows:
433+
<<: *all
434+
ROOT: X:/pipe
435+
"""
436+
output = subprocess.check_output(
437+
command,
438+
shell=True,
439+
universal_newlines=True,
440+
)
441+
self.assertEqual(output, expected_output)
442+
443+
def test_thing(self):
444+
"""Tests baking the thing stack with depth of 1."""
445+
command = (
446+
f"%s thing -o {self.filename} --depth 1; cat {self.filename}"
447+
% self.envstack_bin
448+
)
449+
expected_output = """#!/usr/bin/env envstack
450+
include: [default]
451+
all: &all
452+
<<: *all
453+
CHAR_LIST: [a, b, c, "${HELLO}"]
454+
DICT: {a: 1, b: 2, c: "${INT}"}
455+
FLOAT: 1.0
456+
HELLO: goodbye
457+
INT: 5
458+
LOG_LEVEL: ${LOG_LEVEL:=INFO}
459+
NUMBER_LIST: [1, 2, 3]
460+
darwin:
461+
<<: *all
462+
ROOT: ${HOME}/Library/Application Support/pipe
463+
linux:
464+
<<: *all
465+
ROOT: ${HOME}/.local/pipe
466+
windows:
467+
<<: *all
468+
ROOT: C:/ProgramData/pipe
469+
"""
470+
output = subprocess.check_output(
471+
command,
472+
shell=True,
473+
universal_newlines=True,
474+
)
475+
self.assertEqual(output, expected_output)
476+
477+
def test_default_encrypted(self):
478+
"""Tests baking the default stack encrypted with base64."""
479+
command = (
480+
f"%s --encrypt -o {self.filename}; cat {self.filename}" % self.envstack_bin
481+
)
482+
expected_output = """#!/usr/bin/env envstack
483+
include: []
484+
all: &all
485+
<<: *all
486+
DEPLOY_ROOT: !encrypt JHtST09UfS8ke0VOVn0=
487+
ENV: !encrypt cHJvZA==
488+
ENVPATH: !encrypt JHtERVBMT1lfUk9PVH0vZW52OiR7RU5WUEFUSH0=
489+
HELLO: !encrypt JHtIRUxMTzo9d29ybGR9
490+
LOG_LEVEL: !encrypt JHtMT0dfTEVWRUw6PUlORk99
491+
PATH: !encrypt JHtERVBMT1lfUk9PVH0vYmluOiR7UEFUSH0=
492+
PYTHONPATH: !encrypt JHtERVBMT1lfUk9PVH0vbGliL3B5dGhvbjoke1BZVEhPTlBBVEh9
493+
darwin:
494+
<<: *all
495+
ROOT: !encrypt L1ZvbHVtZXMvcGlwZQ==
496+
linux:
497+
<<: *all
498+
ROOT: !encrypt L21udC9waXBl
499+
windows:
500+
<<: *all
501+
ROOT: !encrypt WDovcGlwZQ==
502+
"""
503+
output = subprocess.check_output(
504+
command,
505+
shell=True,
506+
universal_newlines=True,
507+
)
508+
self.assertEqual(output, expected_output)
509+
510+
def test_thing_encrypted(self):
511+
"""Tests baking the thing stack with depth of 1 excrypted."""
512+
command = (
513+
f"%s thing --encrypt -o {self.filename} --depth 1; cat {self.filename}"
514+
% self.envstack_bin
515+
)
516+
expected_output = """#!/usr/bin/env envstack
517+
include: [default]
518+
all: &all
519+
<<: *all
520+
CHAR_LIST: !encrypt WydhJywgJ2InLCAnYycsICcke0hFTExPfSdd
521+
DICT: !encrypt eydhJzogMSwgJ2InOiAyLCAnYyc6ICcke0lOVH0nfQ==
522+
FLOAT: !encrypt MS4w
523+
HELLO: !encrypt Z29vZGJ5ZQ==
524+
INT: !encrypt NQ==
525+
LOG_LEVEL: !encrypt JHtMT0dfTEVWRUw6PUlORk99
526+
NUMBER_LIST: !encrypt WzEsIDIsIDNd
527+
darwin:
528+
<<: *all
529+
ROOT: !encrypt JHtIT01FfS9MaWJyYXJ5L0FwcGxpY2F0aW9uIFN1cHBvcnQvcGlwZQ==
530+
linux:
531+
<<: *all
532+
ROOT: !encrypt JHtIT01FfS8ubG9jYWwvcGlwZQ==
533+
windows:
534+
<<: *all
535+
ROOT: !encrypt QzovUHJvZ3JhbURhdGEvcGlwZQ==
536+
"""
537+
output = subprocess.check_output(
538+
command,
539+
shell=True,
540+
universal_newlines=True,
541+
)
542+
self.assertEqual(output, expected_output)
543+
544+
def test_blank(self):
545+
"""Tests baking a blank stack."""
546+
command = (
547+
f"%s doesnotexist -o {self.filename}; cat {self.filename}"
548+
% self.envstack_bin
549+
)
550+
expected_output = """#!/usr/bin/env envstack
551+
include: []
552+
all: &all
553+
<<: *all
554+
darwin:
555+
<<: *all
556+
linux:
557+
<<: *all
558+
windows:
559+
<<: *all
560+
"""
561+
output = subprocess.check_output(
562+
command,
563+
shell=True,
564+
universal_newlines=True,
565+
)
566+
self.assertEqual(output, expected_output)
567+
568+
353569
class TestCommands(unittest.TestCase):
354570
"""Tests various envstack commands."""
355571

0 commit comments

Comments
 (0)