Skip to content

Commit

Permalink
adding tests for was_changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Jan 18, 2016
1 parent 325dd87 commit 7c7878c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 23 deletions.
45 changes: 45 additions & 0 deletions tests/rules/test_was_changed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import time
from uranium.rules import rule, WasChanged


def test_unchanged(tmpdir, build):

g = []

tmpdir.join("foo.txt").write("foo")

@build.task
@rule(WasChanged("./foo.txt"))
def main(build):
g.append("ran")

build.run_task("main")
assert "ran" in g
build.run_task("main")
assert len(g) == 1


def test_changed(tmpdir, build):

g = []

f_path = os.path.join(tmpdir.strpath, "foo.txt")

with open(f_path, "w+") as fh:
fh.write("foo")

@build.task
@rule(WasChanged("./foo.txt"))
def main(build):
g.append("ran")

build.run_task("main")
assert "ran" in g
# for some reason we need a sleep
# here. I think it's not considering
# milliseconds.
time.sleep(1)
os.utime(f_path)
build.run_task("main")
assert len(g) == 2
22 changes: 3 additions & 19 deletions uranium/rules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import functools
from abc import ABCMeta, abstractmethod
from ..experimental import experimental
from .base import RuleBase
from .was_changed import WasChanged


class RuleBase(object):
"""
an example of a rule.
* func gets set during the initialization process.
"""
func = None

__metaclass__ = ABCMeta

@abstractmethod
def before(self, build):
pass

@abstractmethod
def after(self, build):
pass
__all__ = ["RuleBase", "WasChanged", "rule"]


@experimental
Expand Down
20 changes: 20 additions & 0 deletions uranium/rules/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from abc import ABCMeta, abstractmethod


class RuleBase(object):
"""
an example of a rule.
* func gets set during the initialization process.
"""
func = None

__metaclass__ = ABCMeta

@abstractmethod
def before(self, build):
pass

@abstractmethod
def after(self, build):
pass
11 changes: 7 additions & 4 deletions uranium/rules/was_changed.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import time
from . import RuleBase
from .base import RuleBase
KEY = "_uranium.rules.was_changed"


Expand All @@ -9,15 +9,18 @@ class WasChanged(RuleBase):
def __init__(self, path):
self._path = path

def path(self, build):
return os.path.join(build.root, self._path)

def before(self, build):
previous_timestamp = build.history.get(self.key)
if not previous_timestamp:
return False
current_timestamp = self._get_timestamp(self.path)
return current_timestamp > previous_timestamp
current_timestamp = self._get_timestamp(self.path(build))
return current_timestamp <= previous_timestamp

def after(self, build):
current_timestamp = self._get_timestamp(self.path)
current_timestamp = self._get_timestamp(self.path(build))
build.history[self.key] = current_timestamp

@property
Expand Down

0 comments on commit 7c7878c

Please sign in to comment.