blob: 84ad3908a8ce1dc54f894ac318718ea8e506de23 [file]
# Copyright 2023 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import json
import os
import tempfile
import unittest
from tests_json_file import TestEntry
from tests_json_file import TestFileError
from tests_json_file import TestSection
class TestFileTest(unittest.TestCase):
"""Test processing tests.json"""
def test_from_file(self) -> None:
"""Test basic loading of a tests.json file."""
contents = [
TestEntry(
test=TestSection(
"my_test",
"//src:my_test",
"linux",
)
).to_dict(), # type:ignore
TestEntry(
test=TestSection(
"my_test2",
"//src:my_test2",
"linux",
)
).to_dict(), # type:ignore
]
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
json.dump(contents, f)
entries: list[TestEntry] = TestEntry.from_file(path)
self.assertListEqual(
[t.test.name for t in entries], ["my_test", "my_test2"]
)
def test_duplicate_name_error(self) -> None:
"""Ensure that loading a tests.json file with duplicate names raises an exception."""
contents = [
TestEntry(
test=TestSection(
"my_test",
"//src:my_test",
"linux",
)
).to_dict(), # type:ignore
TestEntry(
test=TestSection(
"my_test",
"//src:my_test2",
"linux",
)
).to_dict(), # type:ignore
]
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
json.dump(contents, f)
self.assertRaises(TestFileError, lambda: TestEntry.from_file(path))
def test_duplicate_name_path_ok(self) -> None:
"""Ensure that loading a tests.json file with duplicate names but without duplicate paths is OK."""
contents = [
TestEntry(
test=TestSection(
"my_test",
"//src:my_test",
"linux",
path="foo.sh",
)
).to_dict(), # type:ignore
TestEntry(
test=TestSection(
"my_test",
"//src:my_test2",
"linux",
path="bar.sh",
)
).to_dict(), # type:ignore
]
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
json.dump(contents, f)
self.assertEqual(len(TestEntry.from_file(path)), 2)
def test_invalid_format(self) -> None:
"""Ensure an exception is raised if the top-level JSON field is not a list."""
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
f.write("{}")
self.assertRaises(TestFileError, lambda: TestEntry.from_file(path))
def test_invalid_json(self) -> None:
"""Ensure an exception is raised if the file does not contain valid JSON."""
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
f.write("3 {}")
self.assertRaises(
json.JSONDecodeError, lambda: TestEntry.from_file(path)
)
def test_missing_file(self) -> None:
"""Ensure an exception is raised if the file does not exist."""
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
self.assertRaises(IOError, lambda: TestEntry.from_file(path))
def test_needs_package_server_parsing(self) -> None:
"""Ensure needs_package_server field is parsed correctly from tests.json."""
contents = [
{
"test": {
"name": "test_true",
"label": "//src:test_true",
"os": "linux",
},
"needs_package_server": True,
},
{
"test": {
"name": "test_false",
"label": "//src:test_false",
"os": "linux",
},
"needs_package_server": False,
},
{
"test": {
"name": "test_none",
"label": "//src:test_none",
"os": "linux",
},
},
]
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
json.dump(contents, f)
entries = TestEntry.from_file(path)
self.assertTrue(entries[0].needs_package_server)
self.assertFalse(entries[1].needs_package_server)
self.assertIsNone(entries[2].needs_package_server)
def test_ignores_unknown_fields(self) -> None:
"""Ensure unknown properties in tests.json are ignored when parsing."""
contents = [
{
"test": {
"name": "fuchsia-pkg://fuchsia.com/pkg#meta/test.cm",
"label": "//src:pkg",
"os": "linux",
"package_url": "fuchsia-pkg://fuchsia.com/pkg#meta/test.cm",
"path": "pkg_test_test.sh",
"unknown_extra_field": "pkg_test_test.sh",
},
}
]
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
json.dump(contents, f)
entries = TestEntry.from_file(path)
self.assertEqual(len(entries), 1)
self.assertEqual(
entries[0].test.package_url,
"fuchsia-pkg://fuchsia.com/pkg#meta/test.cm",
)
self.assertEqual(entries[0].test.path, "pkg_test_test.sh")
def test_skipped_test_exit_code_parsing(self) -> None:
"""Ensure skipped_test_exit_code field is parsed correctly from tests.json."""
contents = (
{
"test": {
"name": "test_explicit",
"label": "//src:test_explicit",
"os": "linux",
"skipped_test_exit_code": 77,
},
},
{
"test": {
"name": "test_zero",
"label": "//src:test_zero",
"os": "linux",
"skipped_test_exit_code": 0,
},
},
{
"test": {
"name": "test_unset",
"label": "//src:test_unset",
"os": "linux",
},
},
)
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
json.dump(contents, f)
entries = TestEntry.from_file(path)
self.assertEqual(entries[0].test.skipped_test_exit_code, 77)
self.assertEqual(entries[1].test.skipped_test_exit_code, 0)
self.assertIsNone(entries[2].test.skipped_test_exit_code)
def test_package_resolution_timeout_secs_parsing(self) -> None:
"""Ensure package_resolution_timeout_secs field is parsed correctly from tests.json."""
contents = (
{
"test": {
"name": "test_explicit",
"label": "//src:test_explicit",
"os": "linux",
"package_resolution_timeout_secs": 660,
},
},
{
"test": {
"name": "test_zero",
"label": "//src:test_zero",
"os": "linux",
"package_resolution_timeout_secs": 0,
},
},
{
"test": {
"name": "test_unset",
"label": "//src:test_unset",
"os": "linux",
},
},
)
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "tests.json")
with open(path, "w") as f:
json.dump(contents, f)
entries = TestEntry.from_file(path)
self.assertEqual(
entries[0].test.package_resolution_timeout_secs, 660
)
self.assertEqual(entries[1].test.package_resolution_timeout_secs, 0)
self.assertIsNone(entries[2].test.package_resolution_timeout_secs)