Class: Cotta::InMemorySystem

Inherits:
Object
  • Object
show all
Defined in:
lib/cotta/impl/in_memory_system.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInMemorySystem

Returns a new instance of InMemorySystem.



8
9
10
11
12
13
14
15
16
# File 'lib/cotta/impl/in_memory_system.rb', line 8

def initialize
  @executed_commands = []
  @content = ""
  @file_system = Hash.new
  @file_system[nil] = DirectoryContent.new('')
  @file_system[Pathname.new('/')] = DirectoryContent.new('/')
  @file_system[Pathname.new('.')] = DirectoryContent.new('.')
  @output_map = Hash.new
end

Instance Attribute Details

#executed_commandsObject (readonly)

Returns the value of attribute executed_commands.



6
7
8
# File 'lib/cotta/impl/in_memory_system.rb', line 6

def executed_commands
  @executed_commands
end

#pwdObject (readonly)

Returns the value of attribute pwd.



6
7
8
# File 'lib/cotta/impl/in_memory_system.rb', line 6

def pwd
  @pwd
end

Instance Method Details

#chdir(path) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cotta/impl/in_memory_system.rb', line 121

def chdir(path)
  last_pwd = @pwd
  @pwd = path.to_s
  result = 0
  if (block_given?)
    begin
      result = yield
    ensure
      @pwd = last_pwd
    end
  end
  result
end

#check_dir_exists(pathname) ⇒ Object

Raises:

  • (Errno::ENOENT)


59
60
61
# File 'lib/cotta/impl/in_memory_system.rb', line 59

def check_dir_exists(pathname)
  raise Errno::ENOENT, pathname unless dir_exists? pathname
end

#check_file_exists(pathname) ⇒ Object

Raises:

  • (Errno::ENOENT)


63
64
65
# File 'lib/cotta/impl/in_memory_system.rb', line 63

def check_file_exists(pathname)
  raise Errno::ENOENT, pathname unless file_exists? pathname
end

#copy(source, target) ⇒ Object



76
77
78
# File 'lib/cotta/impl/in_memory_system.rb', line 76

def copy(source, target)
  copy_file(source, target)
end

#copy_dir(source, target) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/cotta/impl/in_memory_system.rb', line 94

def copy_dir(source, target)
  check_dir_exists(source)
  mkdir(target)
  path_content(source).children.each do |item|
    item.copy_to_dir(self, source, target)
  end
end

#copy_file(source, target) ⇒ Object



80
81
82
83
# File 'lib/cotta/impl/in_memory_system.rb', line 80

def copy_file(source, target)
  file_content = retrieve_file_content(source, 'r').content
  create_file(target).content = file_content.clone
end

#delete_dir(pathname) ⇒ Object

Raises:

  • (Errno::ENOENT)


112
113
114
115
# File 'lib/cotta/impl/in_memory_system.rb', line 112

def delete_dir(pathname)
  raise Errno::ENOENT.new(pathname) unless dir_exists? pathname
  delete_entry(pathname)
end

#delete_file(pathname) ⇒ Object

Raises:

  • (Errno::ENOENT)


107
108
109
110
# File 'lib/cotta/impl/in_memory_system.rb', line 107

def delete_file(pathname)
  raise Errno::ENOENT.new(pathname) unless file_exists? pathname
  delete_entry(pathname)
end

#dir_exists?(pathname) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/cotta/impl/in_memory_system.rb', line 33

def dir_exists?(pathname)
 content = path_content(pathname)
  return !content.nil? && content.directory?
end

#dir_stat(pathname) ⇒ Object



43
44
45
46
# File 'lib/cotta/impl/in_memory_system.rb', line 43

def dir_stat(pathname)
  check_dir_exists(pathname)
  path_content(pathname).stat
end

#expect_command(command) ⇒ Object



29
30
31
# File 'lib/cotta/impl/in_memory_system.rb', line 29

def expect_command(command)
  Entry.new(command)
end

#file_exists?(pathname) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/cotta/impl/in_memory_system.rb', line 38

def file_exists?(pathname)
  content = path_content(pathname)
  return !content.nil? && content.file?
end

#file_stat(pathname) ⇒ Object



48
49
50
51
# File 'lib/cotta/impl/in_memory_system.rb', line 48

def file_stat(pathname)
  check_file_exists(pathname)
  path_content(pathname).stat
end

#io(*args) ⇒ Object



71
72
73
74
# File 'lib/cotta/impl/in_memory_system.rb', line 71

def io(*args)
  file_content = retrieve_file_content(args[0], args[1])
  return StringIO.new(file_content.content, *args[1, args.size - 1])
end

#list(pathname) ⇒ Object



53
54
55
56
57
# File 'lib/cotta/impl/in_memory_system.rb', line 53

def list(pathname)
  check_dir_exists(pathname)
  content = path_content(pathname)
  return content.children.collect {|item| item.name}
end

#mkdir(pathname) ⇒ Object



67
68
69
# File 'lib/cotta/impl/in_memory_system.rb', line 67

def mkdir(pathname)
  path_content!(pathname.cotta_parent).add(create_dir(pathname))
end

#move(source, target) ⇒ Object



85
86
87
# File 'lib/cotta/impl/in_memory_system.rb', line 85

def move(source, target)
  move_file(source, target)
end

#move_dir(source, target) ⇒ Object



102
103
104
105
# File 'lib/cotta/impl/in_memory_system.rb', line 102

def move_dir(source, target)
  copy_dir(source, target)
  delete_dir(source)
end

#move_file(source, target) ⇒ Object



89
90
91
92
# File 'lib/cotta/impl/in_memory_system.rb', line 89

def move_file(source, target)
  copy(source, target)
  delete_file(source)
end

#output_for_command(command, output) ⇒ Object



25
26
27
# File 'lib/cotta/impl/in_memory_system.rb', line 25

def output_for_command(command, output)
  @output_map[command] = output
end

#shell(command) ⇒ Object



18
19
20
21
22
23
# File 'lib/cotta/impl/in_memory_system.rb', line 18

def shell(command)
  @executed_commands.push(command)
  result = @output_map[command]
  raise "#{command} not found in expectation" unless result
  return result
end

#to_sObject



117
118
119
# File 'lib/cotta/impl/in_memory_system.rb', line 117

def to_s
  return 'InMemorySystem'
end