| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Tests for the artifact definitions readers.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import unittest |
| 6 |
|
| 7 |
from artifacts import reader |
| 8 |
from artifacts import writer |
| 9 |
|
| 10 |
from tests import test_lib |
| 11 |
|
| 12 |
|
| 13 |
class ArtifactsWriterTest(test_lib.BaseTestCase): |
| 14 |
"""Class to test the artifacts writer.""" |
| 15 |
|
| 16 |
def _TestArtifactsConversion( |
| 17 |
self, artifact_reader, artifact_writer, filename): |
| 18 |
"""Tests artifacts conversion. |
| 19 |
|
| 20 |
Args: |
| 21 |
artifact_reader (ArtifactsReader): artifact reader. |
| 22 |
artifact_writer (ArtifactsWriter): artifact writer. |
| 23 |
filename (str): name of the file to convert. |
| 24 |
""" |
| 25 |
test_file = self._GetTestFilePath([filename]) |
| 26 |
self._SkipIfPathNotExists(test_file) |
| 27 |
|
| 28 |
artifact_definitions = list(artifact_reader.ReadFile(test_file)) |
| 29 |
|
| 30 |
with test_lib.TempDirectory() as temporary_directory: |
| 31 |
output_file = os.path.join(temporary_directory, filename) |
| 32 |
|
| 33 |
artifact_writer.WriteArtifactsFile(artifact_definitions, output_file) |
| 34 |
|
| 35 |
converted_artifact_definitions = list( |
| 36 |
artifact_reader.ReadFile(output_file)) |
| 37 |
|
| 38 |
self.assertListEqual( |
| 39 |
[artifact.AsDict() for artifact in artifact_definitions], |
| 40 |
[artifact.AsDict() for artifact in converted_artifact_definitions]) |
| 41 |
|
| 42 |
def testJsonWriter(self): |
| 43 |
"""Tests conversion with the JsonArtifactsWriter.""" |
| 44 |
artifact_reader = reader.JsonArtifactsReader() |
| 45 |
artifact_writer = writer.JsonArtifactsWriter() |
| 46 |
self._TestArtifactsConversion( |
| 47 |
artifact_reader, artifact_writer, 'definitions.json') |
| 48 |
|
| 49 |
def testYamlWriter(self): |
| 50 |
"""Tests conversion with the YamlArtifactsWriter.""" |
| 51 |
artifact_reader = reader.YamlArtifactsReader() |
| 52 |
artifact_writer = writer.YamlArtifactsWriter() |
| 53 |
self._TestArtifactsConversion( |
| 54 |
artifact_reader, artifact_writer, 'definitions.yaml') |
| 55 |
|
| 56 |
|
| 57 |
if __name__ == '__main__': |
| 58 |
unittest.main() |