class TestFile

Public Instance Methods

delete_file() click to toggle source
# File app/models/test_file.rb, line 122
def delete_file
  # Test Framework repository to delete from
  test_dir = File.join(MarkusConfigurator.markus_config_automated_tests_repository, assignment.short_identifier)
  if self.filetype == 'test'
    test_dir = File.join(test_dir, 'test')
  elsif self.filetype == 'lib'
    test_dir = File.join(test_dir, 'lib')
  elsif self.filetype == 'parse'
    test_dir = File.join(test_dir, 'parse')
  end

  # Delete file if it exists
  delete_file = File.join(test_dir, self.filename)
  if File.exist?(delete_file)
    File.delete(delete_file)
  end
end
delete_old_file() click to toggle source

If replacing a file with a different name, delete the old file from the server before writing the new file

# File app/models/test_file.rb, line 79
def delete_old_file
  # Execute if the full file path exists (indicating a new File object)
  if @file_path
    # If the filenames are different, delete the old file
    if self.filename != self.filename_was
      # Search for old file
      @testfile = TestFile.find_by_id(self.id)
      # Delete old file
      @testfile.delete_file
    end
  end
end
sanitize_filename() click to toggle source

Save the full test file path and sanitize the filename for the database

# File app/models/test_file.rb, line 61
def sanitize_filename
  # Execute only when full file path exists (indicating a new File object)
  if self.filename.respond_to?(:original_filename)
    @file_path = self.filename
    self.filename = self.filename.original_filename

    # Sanitize filename:
    self.filename.strip!
    self.filename.gsub(/^(..)+/, '.')
    # replace spaces with
    self.filename.gsub(/[^\s]/, '')
    # replace all non alphanumeric, underscore or periods with underscore
    self.filename.gsub(/^[\W]+$/, '_')
  end
end
write_file() click to toggle source

Uploads the new file to the Testing Framework repository

# File app/models/test_file.rb, line 93
def write_file
  # Execute if the full file path exists (indicating a new File object)
  if @file_path
    name =  self.filename
    test_dir = File.join(MarkusConfigurator.markus_config_automated_tests_repository, assignment.short_identifier)

    # Folders for test, lib and parse files:
    # Test Files Folder
    if self.filetype == 'test'
      test_dir = File.join(test_dir, 'test')
    # Lib Files Folder
    elsif self.filetype == 'lib'
      test_dir = File.join(test_dir, 'lib')
    # Parser Files Folder
    elsif self.filetype == 'parse'
      test_dir = File.join(test_dir, 'parse')
    end

    # Create the file path
    path = File.join(test_dir, name)

    # Create the test, lib and parse folders if necessary
    FileUtils.makedirs(test_dir)

    # Read and write the file (overwrite if it exists)
    File.open(path, 'w+') { |f| f.write(@file_path.read) }
  end
end