Skip to content

Commit

Permalink
Allow linesep used by Line() to be controlled
Browse files Browse the repository at this point in the history
  • Loading branch information
Jc2k committed Apr 12, 2015
1 parent 0ef891b commit 93e057f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 1 addition & 2 deletions fuselage/providers/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

from fuselage import error, resources, provider, platform
Expand All @@ -28,7 +27,7 @@ def apply(self):
return

lines = force_str(platform.get(self.resource.name)).splitlines()
contents = os.linesep.join(line for line in self.filter_lines(lines))
contents = self.resource.linesep.join(line for line in self.filter_lines(lines))

fc = EnsureContents(
self.resource.name,
Expand Down
5 changes: 5 additions & 0 deletions fuselage/resources/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
both the metadata associated with the file (for example owner and permission)
and the contents of the files themselves. """

import os

from fuselage.resource import Resource
from fuselage.policy import (
Policy,
Expand Down Expand Up @@ -57,6 +59,9 @@ def implicit_id(self):
match = String(default="")
""" The python regular expression to match the line to be updated. """

linesep = String(default=os.linesep)
""" The delimiter used to mark a line. \\n on posix. """

sensitive = Boolean(default=False)


Expand Down
9 changes: 9 additions & 0 deletions fuselage/tests/test_providers_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_target_doesnt_exist(self):
name="/target_doesnt_exist",
match="^FOO",
line="FOO 2",
linesep="\n",
))
self.assertRaises(error.PathComponentMissing, self.apply)

Expand All @@ -33,6 +34,7 @@ def test_replace_existing_line_start(self):
name="/replace_existing_line_start",
match="^FOO",
line="FOO 2",
linesep="\n",
))
self.check_apply()
self.assertEqual(platform.get("/replace_existing_line_start"), "FOO 2\nBAR 2\nBAZ 3")
Expand All @@ -43,6 +45,7 @@ def test_replace_existing_line_middle(self):
name="/replace_existing_line_middle",
match="^BAR",
line="BAR 1",
linesep="\n",
))
self.check_apply()
self.assertEqual(platform.get("/replace_existing_line_middle"), "FOO 1\nBAR 1\nBAZ 3")
Expand All @@ -53,6 +56,7 @@ def test_replace_existing_line_end(self):
name="/replace_existing_line_end",
match="^BAZ",
line="BAZ 2",
linesep="\n",
))
self.check_apply()
self.assertEqual(platform.get("/replace_existing_line_end"), "FOO 1\nBAR 2\nBAZ 2")
Expand All @@ -63,6 +67,7 @@ def test_replace_existing_line_append(self):
name="/replace_existing_line_append",
match="^QUX",
line="QUX 2",
linesep="\n",
))
self.check_apply()
self.assertEqual(platform.get("/replace_existing_line_append"), "FOO 1\nBAR 2\nBAZ 3\nQUX 2")
Expand All @@ -73,6 +78,7 @@ def test_remove_line(self):
name="/replace_existing_line_append",
policy="remove",
match="^BAR",
linesep="\n",
))
self.check_apply()
self.assertEqual(platform.get("/replace_existing_line_append"), "FOO 1\nBAZ 3")
Expand All @@ -83,16 +89,19 @@ def test_multiple_lines(self):
name="/multiple_lines",
match="^BAR",
line="BAR 3",
linesep="\n",
))
self.bundle.add(Line(
name="/multiple_lines",
match="^BAZ",
line="BAZ 4",
linesep="\n",
))
self.bundle.add(Line(
name="/multiple_lines",
match="^FOO",
line="FOO 2",
linesep="\n",
))
self.check_apply()
self.assertEqual(platform.get("/multiple_lines"), "FOO 2\nBAR 3\nBAZ 4")

0 comments on commit 93e057f

Please sign in to comment.