Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/linux_admin/fstab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ def write!
private

def read
contents = File.read('/etc/fstab')
contents = contents.lines.to_a
contents.reject! { |line|
first_char = line.strip[0]
first_char == '#' || first_char =~ /\s/
}
contents
File.read('/etc/fstab').lines.find_all {|line| !line.blank? && !line.strip.starts_with?("#")}
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrafanie I think the reject is harder to follow than a collect. Also, since we have active support, shoun't we use .starts_with? and .blank??

Just a thought...
contents.lines.collect {|line| line.strip!; line unless line.blank? || line.starts_with?("#")}.compact

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brandondunne The only benefit to using blank? in your example is if we're keeping whitespace.
line.strip! effectively makes the line.blank? into a .nil? call since we've removed the possibility for empty strings.

If we were going to use blank?, I would think something like this:

contents.lines.to_a.collect {|line| line unless line.blank? || line.starts_with?("#")}.compact

This retains any leading or trailing whitespace in the line though.

Either way, I think the real solution is to teach the parsing and writing code to only make changes where you make changes in the FSTabEntry objects.


def refresh
Expand Down
19 changes: 19 additions & 0 deletions spec/fstab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@
require 'stringio'

describe LinuxAdmin::FSTab do
before do
# Reset the singleton so subsequent tests get a new instance
Singleton.send :__init__, LinuxAdmin::FSTab
end

it "newline, single spaces, tab" do
fstab = <<eos



eos
File.should_receive(:read).with('/etc/fstab').and_return(fstab)
LinuxAdmin::FSTab.instance.entries.size.should == 0
end

it "creates FSTabEntry for each line in fstab" do
fstab = <<eos
# Comment, indented comment, comment with device information
# /dev/sda1 / ext4 defaults 1 1
# /dev/sda1 / ext4 defaults 1 1
/dev/sda1 / ext4 defaults 1 1
/dev/sda2 swap swap defaults 0 0
eos
Expand Down Expand Up @@ -36,6 +54,7 @@
entry.mount_options = 'defaults'
entry.dumpable = 1
entry.fsck_order = 1
LinuxAdmin::FSTab.any_instance.stub(:refresh) # don't read /etc/fstab
LinuxAdmin::FSTab.instance.entries = [entry]

File.should_receive(:write).with('/etc/fstab', "/dev/sda1 / ext4 defaults 1 1\n")
Expand Down