Пример #1
0
  def testAddImportToInit(self):
    """Tests adding a line to an init file."""
    test_path = os.path.join(os.path.dirname(
        os.path.dirname(os.path.dirname(__file__))), 'test_data')
    test_file = os.path.join(test_path, 'test_init.py')

    with open(test_file, 'r') as fh:
      test_file_content = fh.read()

    new_import = 'from secret.project.parsers import foobar\n'

    temp_file = tempfile.NamedTemporaryFile()
    with open(temp_file.name, 'w') as fh:
      fh.write(test_file_content)

    handler = file_handler.FileHandler()
    handler.AddImportToInit(temp_file.name, new_import)

    with open(temp_file.name, 'r') as fh:
      content = fh.read()
    os.remove(temp_file.name)

    expected_file_path = os.path.join(test_path, 'test_init_fixed.py')
    with open(expected_file_path, 'r') as fh:
      expected_content = fh.read()

    self.assertEqual(content, expected_content)
Пример #2
0
 def testCreateFileFromPath(self):
   """Tests if the creation of a file none existing beforehand works."""
   handler = file_handler.FileHandler()
   with tempfile.TemporaryDirectory() as directory:
     source = os.path.join(directory, self.file)
     self.assertFalse(os.path.exists(source))
     handler.CreateFileFromPath(source)
     self.assertTrue(os.path.exists(source))
Пример #3
0
 def testCreateFile(self):
   """Tests if the creation of a file none existing beforehand works."""
   with tempfile.TemporaryDirectory() as directory:
     handler = file_handler.FileHandler()
     file_path = os.path.join(directory, self.file)
     self.assertFalse(os.path.exists(file_path))
     handler.CreateFile(directory, self.name, self.suffix)
     self.assertTrue(os.path.exists(file_path))
Пример #4
0
 def testCreateFileIfPathNotExisting(self):
   """Tests creation of non-existing file."""
   with tempfile.TemporaryDirectory() as directory:
     new_path = os.path.join(directory, "temp")
     handler = file_handler.FileHandler()
     file_path = os.path.join(new_path, self.file)
     self.assertFalse(os.path.exists(file_path))
     handler.CreateFile(new_path, self.name, self.suffix)
     self.assertTrue(os.path.exists(file_path))
Пример #5
0
 def testCreateFolder(self):
   """Tests if the creation of a folder works."""
   with tempfile.TemporaryDirectory() as directory:
     new_path = os.path.join(directory, "newfolder")
     self.assertFalse(os.path.exists(new_path))
     handler = file_handler.FileHandler()
     handler._CreateFolder(new_path)  # pylint: disable=protected-access
     actual = os.path.exists(new_path)
   self.assertTrue(actual)
Пример #6
0
 def __init__(self):
     """Initializes the engine."""
     super(ScaffolderEngine, self).__init__()
     self._attributes = {}
     self._definition = ''
     self._definition_root_path = ''
     self._file_handler = file_handler.FileHandler()
     self._file_name_prefix = ''
     self._scaffolder = None
     self.module_name = ''
Пример #7
0
  def testCopyFile(self):
    """Tests if the copying of a file none existing beforehand works."""
    expected_content = "this is test content."

    with tempfile.TemporaryDirectory() as directory:
      source = os.path.join(directory, self.file)
      destination = os.path.join(directory, "copy", self.file)

      with open(source, "a") as f:
        f.write(expected_content)

      handler = file_handler.FileHandler()
      self.assertFalse(os.path.exists(destination))
      handler.CopyFile(source, destination)
      self.assertTrue(os.path.exists(destination))
      self.assertTrue(filecmp.cmp(destination, source))
Пример #8
0
  def testAddContentIfFileDoesNotExist(self):
    """Tests if the editing of a file not existing works."""
    content = "this is test content. "
    expected = content

    with tempfile.TemporaryDirectory() as directory:
      source = os.path.join(directory, self.file)
      handler = file_handler.FileHandler()
      self.assertFalse(os.path.exists(source))
      handler.AddContent(source, content)
      self.assertTrue(os.path.exists(source))

      with open(source, "r") as f:
        actual = f.read()

    self.assertEqual(expected, actual)
Пример #9
0
  def testAddContentIfFileAndFolderDoesNotExist(self):
    """Tests creation or modification of non-existing file with content."""
    content = "this is test content. "
    expected = content

    with tempfile.TemporaryDirectory() as directory:
      new_path = os.path.join(directory, "newfolder")
      source = os.path.join(new_path, self.file)
      handler = file_handler.FileHandler()
      self.assertFalse(os.path.exists(source))
      handler.CreateOrModifyFileWithContent(source, content)
      self.assertTrue(os.path.exists(source))

      with open(source, "r") as f:
        actual = f.read()

    self.assertEqual(expected, actual)
Пример #10
0
  def testCreateOrModifyFileWithContentIfFileExists(self):
    """Tests creation or modification of existing file with content."""
    content = "this is test content. "
    expected = content + content

    with tempfile.TemporaryDirectory() as directory:
      source = os.path.join(directory, self.file)
      with open(source, "a") as f:
        f.write(content)

      handler = file_handler.FileHandler()
      self.assertTrue(os.path.exists(source))
      handler.CreateOrModifyFileWithContent(source, content)
      self.assertTrue(os.path.exists(source))

      with open(source, "r") as f:
        actual = f.read()

    self.assertEqual(expected, actual)