Skip to content

Commit

Permalink
fix(shell): Slufigy environment variable names
Browse files Browse the repository at this point in the history
Close #120
  • Loading branch information
Toilal committed Dec 15, 2020
1 parent 00d62b9 commit 0513038
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ History
- Configuration: Add `core.configuration.extra` to load additional configuration files.
- Configuration: Allow merging from default values
- Configuration: Add `insert`/`insert_if_missing` strategies for
- Shell: Allow special characters from configuration to be properly exported in shell.

1.2.3 (2020-11-13)
------------------
Expand Down
17 changes: 13 additions & 4 deletions ddb/feature/shell/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from abc import ABC, abstractmethod
from typing import Tuple, Iterable, Dict, Any, Optional

from slugify import slugify
from ddb.binary import Binary
from ddb.utils.file import force_remove, write_if_different, chmod

Expand Down Expand Up @@ -89,11 +90,15 @@ class BashShellIntegration(ShellIntegration):
def __init__(self):
super().__init__("bash", "Bash")

@staticmethod
def _sanitize_key(key):
return slugify(key, regex_pattern=r'[^-a-zA-Z0-9_]+').upper()

def set_environment_variable(self, key, value):
yield "export " + key + "=" + shlex.quote(value)
yield "export " + self._sanitize_key(key) + "=" + shlex.quote(value)

def remove_environment_variable(self, key):
yield "unset " + key
yield "unset " + self._sanitize_key(key)

def remove_all_binary_shims(self, shims_path: str):
shims = []
Expand Down Expand Up @@ -152,12 +157,16 @@ class CmdShellIntegration(ShellIntegration):
def __init__(self):
super().__init__("cmd", "Windows cmd.exe")

@staticmethod
def _sanitize_key(key):
return slugify(key, regex_pattern=r'[^-a-zA-Z0-9_]+').upper()

def set_environment_variable(self, key, value):
# TODO: Maybe use subprocess.list2cmdline for Windows ?
yield "set " + key + "=" + value.replace('\n', '!NL! ^\n')
yield "set " + self._sanitize_key(key) + "=" + value.replace('\n', '!NL! ^\n')

def remove_environment_variable(self, key):
yield "set " + key + "="
yield "set " + self._sanitize_key(key) + "="

def remove_all_binary_shims(self, shims_path: str):
shims = []
Expand Down
3 changes: 3 additions & 0 deletions tests/it/test_shell_it.data/export-special-chars/ddb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
permissions:
specs:
"bin/dependency-check": "+x"
25 changes: 25 additions & 0 deletions tests/it/test_shell_it.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import re
from pathlib import Path

import pytest
from _pytest.capture import CaptureFixture
from ddb.__main__ import main


@pytest.mark.skipif("os.name == 'nt'")
class TestBashShell:
def test_export_special_chars(self, capsys: CaptureFixture, project_loader):
project_loader("export-special-chars")

main(["configure"])

main(["activate"])

outerr = capsys.readouterr()
script_lines = Path(outerr.out.split()[1].strip()).read_text().splitlines()
matches = [re.match('export (.+)=.*$', x) for x in script_lines]
matches = [x for x in matches if x]

for match in matches:
if match:
assert '/' not in match.group(1)

0 comments on commit 0513038

Please sign in to comment.