Skip to content

Commit

Permalink
Merge pull request #12 from jacebrowning/links
Browse files Browse the repository at this point in the history
Implement symbolic link creation
  • Loading branch information
jacebrowning committed Feb 23, 2015
2 parents 32fdcd3 + 9b62e8a commit 216cf2d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*/test/files/src

# Temporary Python files
*.pyc
*.egg-info
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ifndef TEST_RUNNER
# options are: nose, pytest
TEST_RUNNER := pytest
endif
UNIT_TEST_COVERAGE := 83
UNIT_TEST_COVERAGE := 82
INTEGRATION_TEST_COVERAGE := 96

# Project settings
Expand Down
14 changes: 12 additions & 2 deletions gdm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ def __str__(self):
fmt += " <- '{s}'"
return fmt.format(r=self.repo, v=self.rev, d=self.dir, s=self.link)

def get(self):
def update_files(self):
"""Ensure the source matches the specified revision."""
log.info("updating source files...")

# Fetch the latest changes and revert the working tree if it exists
if os.path.exists(self.dir):
Expand All @@ -57,6 +58,14 @@ def get(self):
# Update the working tree to the specified revision
self.git_update(self.rev)

def create_link(self, root):
"""Create a link from the target name to the current directory."""
if self.link:
log.info("creating a symbolic link...")
target = os.path.join(root, self.link)
source = os.path.relpath(os.getcwd(), os.path.dirname(target))
self.ln(source, target)


@yorm.map_attr(all=Source)
class Sources(yorm.container.List):
Expand Down Expand Up @@ -101,7 +110,8 @@ def install_deps(self):
for source in self.sources:

source.indent = self.indent + self.INDENT
source.get()
source.update_files()
source.create_link(self.root)
count += 1
print()

Expand Down
5 changes: 4 additions & 1 deletion gdm/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def cd(self, path, visible=True):
self._call('cd', path, visible=visible)

def ln(self, source, target):
self._call('ln', '-sf', source, target)
dirpath = os.path.dirname(target)
if not os.path.isdir(dirpath):
self.mkdir(dirpath)
self._call('ln', '-s', '-f', '-F', source, target)


class GitMixin(_Base):
Expand Down
8 changes: 5 additions & 3 deletions gdm/test/test_all.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Integration tests for the `gdm` package."""

import os
import subprocess
import shutil

import pytest

Expand All @@ -15,14 +15,16 @@
def test_install():
"""Verify dependencies can be installed."""
config = Config(FILES)
subprocess.call(['rm', '-rf', config.location])
if os.path.exists(config.location):
shutil.rmtree(config.location)
assert not os.path.exists(config.location)

# clean install
assert gdm.install(FILES)
assert os.path.isdir(config.location)

# second install
assert gdm.install(FILES)
assert 'gdm_1' in os.listdir(config.location)
assert 'gdm_2' in os.listdir(config.location)

shutil.rmtree(os.path.join(FILES, 'src'))
5 changes: 5 additions & 0 deletions gdm/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def test_init_location(self):
assert 'gdm.yml' == config.filename
assert '.gdm' == config.location

def test_path(self):
"""Verify a configuration's path is correct."""
config = Config('mock/root')
assert "mock/root/gdm.yml" == config.path


class TestInstall:

Expand Down
12 changes: 10 additions & 2 deletions gdm/test/test_shell.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Unit tests for the `shell` module."""
# pylint: disable=R0201

from unittest.mock import patch
from unittest.mock import patch, Mock

import pytest

Expand Down Expand Up @@ -67,10 +67,18 @@ def test_cd(self, mock_call):
self.shell.cd('mock/dir/path')
self.assert_calls(mock_call, ["cd mock/dir/path"])

@patch('os.path.isdir', Mock(return_value=True))
def test_ln(self, mock_call):
"""Verify the commands to create symbolic links."""
self.shell.ln('mock/target', 'mock/source')
self.assert_calls(mock_call, ["ln -sf mock/target mock/source"])
self.assert_calls(mock_call, ["ln -s -f -F mock/target mock/source"])

@patch('os.path.isdir', Mock(return_value=False))
def test_ln_missing_parent(self, mock_call):
"""Verify the commands to create symbolic links (missing parent)."""
self.shell.ln('mock/target', 'mock/source')
self.assert_calls(mock_call, ["mkdir -p mock",
"ln -s -f -F mock/target mock/source"])


@patch('gdm.shell._call')
Expand Down

0 comments on commit 216cf2d

Please sign in to comment.