Commit 7d00d8a9 authored by Bella Bah's avatar Bella Bah Committed by Commit Bot

Renamed parser.py to generator_utils to prevent ImportError

Was causing Windows trybot traffic_annotation tests to fail because of
module naming collision.
https://logs.chromium.org/logs/chromium/buildbucket/cr-buildbucket.appspot.com/8872768115210425728/+/steps/test_traffic_annotation_auditor/0/stdout

Change-Id: I5205f8232c633cb9028de667472f4e80f869d973
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2339941Reviewed-by: default avatarNicolas Ouellet-Payeur <nicolaso@chromium.org>
Commit-Queue: Mohamadou Bella Bah <bellabah@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796069}
parent a6f2d030
......@@ -43,11 +43,11 @@ all network traffic annotations specified within `summary/grouping.xml`.
# update_annotations_doc_tests.py
Unit tests for update_annotations_doc.py.
# parser.py
# generator_utils.py
Parses the `grouping.xml` and `annotations.tsv` files to provide
`update_annotations_doc.py` with the annotations and their relevant information,
e.g. unique_id, data, trigger, etc. Also includes methods to parse the json
object returned by the Google Docs API `get()` method.
# parser_tests.py
Unit tests for parser.py.
\ No newline at end of file
# generator_utils_tests.py
Unit tests for generator_utils.py.
\ No newline at end of file
......@@ -22,7 +22,6 @@ import sys
import io
import re
TrafficAnnotation = namedtuple(
"TrafficAnnotation",
["unique_id", "description", "trigger", "data", "settings", "policy"])
......@@ -34,6 +33,7 @@ class Placeholder(str, enum.Enum):
ANNOTATION = "annotation"
ANNOTATION_BOLD = "annotation_bold"
PLACEHOLDER_STYLES = {
Placeholder.GROUP: {
"bold": False,
......@@ -195,7 +195,9 @@ class XMLParser:
"NA")
return {
"type": Placeholder.ANNOTATION, "traffic_annotation": traffic_annotation}
"type": Placeholder.ANNOTATION,
"traffic_annotation": traffic_annotation
}
def build_placeholders(self):
"""
......
......@@ -2,23 +2,22 @@
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Unit tests for parser.py
Unit tests for generator_utils.py
"""
import unittest
import parser
import os
import unittest
import generator_utils
# Absolute path to chrome/src.
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "../../.."))
TESTS_DIR = os.path.join(SCRIPT_DIR, "test_data")
class ParserTest(unittest.TestCase):
TSV_CONTENTS = [
[
TSV_CONTENTS = [[
u"unique_id_A", u"", u"sender_A", u"description_A", u"trigger_A",
u"data_A", u"destination_A", u"cookies_allowed_A", u"cookies_store_A",
u"settings_A", u"chrome_policy_A", u"", u"source_file_A",
......@@ -36,85 +35,96 @@ class ParserTest(unittest.TestCase):
]
ANNOTATIONS_MAPPING = {
"unique_id_A": parser.TrafficAnnotation(**{
"unique_id": "unique_id_A",
"description": "description_A",
"trigger": "trigger_A",
"data": "data_A",
"settings": "settings_A",
"policy": "chrome_policy_A"}),
"unique_id_B": parser.TrafficAnnotation(**{
"unique_id": "unique_id_B",
"description": "description_B",
"trigger": "trigger_B",
"data": "data_B",
"settings": "settings_B",
"policy": "chrome_policy_B"}),
"unique_id_C": parser.TrafficAnnotation(**{
"unique_id": "unique_id_C",
"description": "description_C",
"trigger": "trigger_C",
"data": "data_C",
"settings": "settings_C",
"policy": "chrome_policy_C"})
"unique_id_A":
generator_utils.TrafficAnnotation(
**{
"unique_id": "unique_id_A",
"description": "description_A",
"trigger": "trigger_A",
"data": "data_A",
"settings": "settings_A",
"policy": "chrome_policy_A"
}),
"unique_id_B":
generator_utils.TrafficAnnotation(
**{
"unique_id": "unique_id_B",
"description": "description_B",
"trigger": "trigger_B",
"data": "data_B",
"settings": "settings_B",
"policy": "chrome_policy_B"
}),
"unique_id_C":
generator_utils.TrafficAnnotation(
**{
"unique_id": "unique_id_C",
"description": "description_C",
"trigger": "trigger_C",
"data": "data_C",
"settings": "settings_C",
"policy": "chrome_policy_C"
})
}
PLACEHOLDERS = [
{"type": parser.Placeholder.GROUP, "name": "Group A"},
{"type": parser.Placeholder.SENDER, "name": "Sender 1"},
{"type": generator_utils.Placeholder.GROUP, "name": "Group A"},
{"type": generator_utils.Placeholder.SENDER, "name": "Sender 1"},
{
"type": parser.Placeholder.ANNOTATION,
"type": generator_utils.Placeholder.ANNOTATION,
"traffic_annotation": ANNOTATIONS_MAPPING["unique_id_A"]},
{"type": parser.Placeholder.SENDER, "name": "Sender 2"},
{"type": generator_utils.Placeholder.SENDER, "name": "Sender 2"},
{
"type": parser.Placeholder.ANNOTATION,
"type": generator_utils.Placeholder.ANNOTATION,
"traffic_annotation": ANNOTATIONS_MAPPING["unique_id_B"]},
{"type": parser.Placeholder.GROUP, "name": "Group C"},
{"type": parser.Placeholder.SENDER, "name": "Sender 3"},
{"type": generator_utils.Placeholder.GROUP, "name": "Group C"},
{"type": generator_utils.Placeholder.SENDER, "name": "Sender 3"},
{
"type": parser.Placeholder.ANNOTATION,
"type": generator_utils.Placeholder.ANNOTATION,
"traffic_annotation": ANNOTATIONS_MAPPING["unique_id_C"]}
]
# Document formatted according to fake_grouping.xml
DOC_JSON = parser.extract_body(
target="all", json_file_path=os.path.join(TESTS_DIR, "fake_doc.json"))
DOC_JSON = generator_utils.extract_body(target="all",
json_file_path=os.path.join(
TESTS_DIR, "fake_doc.json"))
def test_load_tsv_file(self):
self.assertEqual(self.TSV_CONTENTS, parser.load_tsv_file(os.path.join(
SRC_DIR,
self.assertEqual(self.TSV_CONTENTS, generator_utils.load_tsv_file(
os.path.join(SRC_DIR,
"tools/traffic_annotation/scripts/test_data/fake_annotations.tsv"),
False))
def test_map_annotations(self):
self.assertEqual(
self.ANNOTATIONS_MAPPING, parser.map_annotations(self.TSV_CONTENTS))
self.assertEqual(self.ANNOTATIONS_MAPPING,
generator_utils.map_annotations(self.TSV_CONTENTS))
def test_xml_parser_build_placeholders(self):
xml_parser = parser.XMLParser(
os.path.join(TESTS_DIR, "fake_grouping.xml"), self.ANNOTATIONS_MAPPING)
xml_parser = generator_utils.XMLParser(
os.path.join(TESTS_DIR, "fake_grouping.xml"), self.ANNOTATIONS_MAPPING)
self.assertEqual(self.PLACEHOLDERS, xml_parser.build_placeholders())
def test_find_first_index(self):
first_index = parser.find_first_index(self.DOC_JSON)
first_index = generator_utils.find_first_index(self.DOC_JSON)
self.assertEqual(1822, first_index)
def test_find_last_index(self):
last_index = parser.find_last_index(self.DOC_JSON)
last_index = generator_utils.find_last_index(self.DOC_JSON)
self.assertEqual(2066, last_index)
def test_find_chrome_browser_version(self):
current_version = parser.find_chrome_browser_version(self.DOC_JSON)
current_version = generator_utils.find_chrome_browser_version(self.DOC_JSON)
self.assertEqual("86.0.4187.0", current_version)
def test_find_bold_ranges(self):
expected_bold_ranges = [
(1843, 1855), (1859, 1867), (1871, 1876), (1880, 1889), (1893, 1900),
(1918, 1930), (1934, 1942), (1968, 1975), (1946, 1951), (1955, 1964),
(2001, 2013), (2017, 2025), (2029, 2034), (2038, 2047), (2051, 2058)]
bold_ranges = parser.find_bold_ranges(self.DOC_JSON)
expected_bold_ranges = [(1843, 1855), (1859, 1867), (1871, 1876),
(1880, 1889), (1893, 1900), (1918, 1930),
(1934, 1942), (1968, 1975), (1946, 1951),
(1955, 1964), (2001, 2013), (2017, 2025),
(2029, 2034), (2038, 2047), (2051, 2058)]
bold_ranges = generator_utils.find_bold_ranges(self.DOC_JSON)
self.assertItemsEqual(expected_bold_ranges, bold_ranges)
if __name__ == "__main__":
unittest.main()
\ No newline at end of file
unittest.main()
......@@ -28,9 +28,9 @@ from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import parser
from parser import (XMLParser, map_annotations, load_tsv_file, Placeholder,
PLACEHOLDER_STYLES)
import generator_utils
from generator_utils import (XMLParser, map_annotations, load_tsv_file,
Placeholder, PLACEHOLDER_STYLES)
# Absolute path to chrome/src.
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
......@@ -167,7 +167,7 @@ class NetworkTrafficAnnotationsDoc:
version = ".".join(line.strip().split("=")[1]
for line in version_file.readlines())
current_version = parser.find_chrome_browser_version(doc)
current_version = generator_utils.find_chrome_browser_version(doc)
replacement = "Chrome Browser version {}".format(version)
target = "Chrome Browser version {}".format(current_version)
......@@ -195,8 +195,8 @@ class NetworkTrafficAnnotationsDoc:
Return: Integer of where to start writing, i.e. the index.
"""
print("Overwriting the destination document.")
first_index = parser.find_first_index(doc)
last_index = parser.find_last_index(doc)
first_index = generator_utils.find_first_index(doc)
last_index = generator_utils.find_last_index(doc)
if self.verbose:
print("First index, last index", first_index, last_index)
......@@ -277,7 +277,7 @@ class NetworkTrafficAnnotationsDoc:
def _format_text(self, start_index, end_index, placeholder_type):
"""Format the text in between |start_index| and |end_index| using the styles
specified by |parser.PLACEHOLDER_STYLES|.
specified by |generator_utils.PLACEHOLDER_STYLES|.
Returns: The request to format the text in between |start_index| and
|end_index|.
......@@ -306,7 +306,7 @@ class NetworkTrafficAnnotationsDoc:
def _create_group_or_sender_request(self, text, index, placeholder_type):
"""Returns the request for inserting the group or sender placeholders using
the styling of |parser.PLACEHOLDER_STYLES|.
the styling of |generator_utils.PLACEHOLDER_STYLES|.
"""
assert placeholder_type in [Placeholder.GROUP, Placeholder.SENDER]
text += "\n"
......@@ -336,7 +336,7 @@ class NetworkTrafficAnnotationsDoc:
the template document for a visual.
Args:
traffic_annotation: parser.TrafficAnnotation
traffic_annotation: generator_utils.TrafficAnnotation
The TrafficAnnotation object with all the relevant information, e.g.
unique_id, description, etc.
index: int
......@@ -473,7 +473,7 @@ class NetworkTrafficAnnotationsDoc:
doc = self._get_doc_contents(self.destination_id)
# the ranges to bold using the updateTextStyle request
bold_ranges = parser.find_bold_ranges(doc)
bold_ranges = generator_utils.find_bold_ranges(doc)
reqs = []
for i, (start_index, end_index) in enumerate(bold_ranges):
if end_index > start_index:
......
......@@ -15,7 +15,7 @@ from mock import MagicMock
# Mock some imports which aren't necessary during testing.
sys.modules["infra_libs"] = MagicMock()
import update_annotations_doc
import parser
import generator_utils
# Absolute path to chrome/src.
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
......@@ -30,7 +30,7 @@ class UpdateAnnotationsDocTest(unittest.TestCase):
def test_create_group_request(self):
text = "TestGroup"
req, index = self.network_doc_obj._create_group_or_sender_request(
text, 0, parser.Placeholder.GROUP)
text, 0, generator_utils.Placeholder.GROUP)
self.assertEqual(len(text)+1, index)
expected_req = [
......@@ -63,7 +63,7 @@ class UpdateAnnotationsDocTest(unittest.TestCase):
text = "TestSender"
print(text)
req, index = self.network_doc_obj._create_group_or_sender_request(
text, 0, parser.Placeholder.SENDER)
text, 0, generator_utils.Placeholder.SENDER)
self.assertEqual(len(text)+1, index)
expected_req = [
......@@ -89,13 +89,15 @@ class UpdateAnnotationsDocTest(unittest.TestCase):
self.assertEqual(expected_req, req)
def test_create_annotation_request(self):
traffic_annotation = parser.TrafficAnnotation(**{
"unique_id": "unique_id_A",
"description": "description_A",
"trigger": "trigger_A",
"data": "data_A",
"settings": "settings_A",
"policy": "chrome_policy_A"})
traffic_annotation = generator_utils.TrafficAnnotation(
**{
"unique_id": "unique_id_A",
"description": "description_A",
"trigger": "trigger_A",
"data": "data_A",
"settings": "settings_A",
"policy": "chrome_policy_A"
})
req, index = self.network_doc_obj._create_annotation_request(
traffic_annotation, 0)
......@@ -172,4 +174,4 @@ class UpdateAnnotationsDocTest(unittest.TestCase):
if __name__ == "__main__":
unittest.main()
\ No newline at end of file
unittest.main()
......@@ -29,7 +29,7 @@ from infra_libs import luci_auth
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from parser import load_tsv_file
from generator_utils import load_tsv_file
class SheetEditor():
......
......@@ -58,4 +58,4 @@ wheel: <
wheel: <
name: "infra/python/wheels/enum34-py2"
version: "version:1.1.6"
>
\ No newline at end of file
>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment