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