Skip to content

Commit 4fabf88

Browse files
Merge pull request #666 from mozilla/philimon/l10n_flow_optimize
Philimon/l10n_workflow_optimize
2 parents 1adc7a2 + 97c9426 commit 4fabf88

File tree

6 files changed

+165
-39
lines changed

6 files changed

+165
-39
lines changed

.github/workflows/l10n.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: L10N Test Execution
33

44
run-name: ${{ github.actor }} is running l10n tests
55
on:
6+
pull_request:
67
workflow_call:
78
inputs:
89
channel:
@@ -37,7 +38,7 @@ env:
3738

3839
jobs:
3940
L10N-MacOS:
40-
if: ${{ inputs.job_to_run == 'L10N-MacOS' || inputs.mac_installer_link }}
41+
if: ${{ inputs.job_to_run == 'L10N-MacOS' || github.event_name == 'pull_request' || inputs.mac_installer_link }}
4142
runs-on: macos-latest
4243
steps:
4344
- name: Create app token
@@ -85,7 +86,7 @@ jobs:
8586
run: |
8687
"$FX_EXECUTABLE" --version;
8788
echo "0" > TEST_EXIT_CODE;
88-
pipenv run python check_l10n_test_cases.py;
89+
pipenv run python choose_l10n_ci_set.py;
8990
while IFS= read -r line; do
9091
echo "Running tests for: $line";
9192
pipenv run python -m l10n_CM.run_l10n --fx-executable="$FX_EXECUTABLE" $line || SCRIPT_EXIT_CODE=$?;
@@ -121,7 +122,7 @@ jobs:
121122
name: artifacts-mac
122123
path: artifacts-mac
123124
L10N-Linux:
124-
if: ${{ inputs.job_to_run == 'L10N-Linux' || inputs.linux_tarball_link }}
125+
if: ${{ inputs.job_to_run == 'L10N-Linux' || github.event_name == 'pull_request' || inputs.linux_tarball_link }}
125126
runs-on: ubuntu-latest
126127
steps:
127128
- name: Create app token
@@ -166,7 +167,7 @@ jobs:
166167
run: |
167168
"$FX_EXECUTABLE" --version;
168169
echo "0" > TEST_EXIT_CODE;
169-
pipenv run python check_l10n_test_cases.py;
170+
pipenv run python choose_l10n_ci_set.py;
170171
Xvfb :99 -screen 0 '1600x1200x24' > artifacts/xvfb.log &
171172
while IFS= read -r line; do
172173
echo "Running tests for: $line";
@@ -203,7 +204,7 @@ jobs:
203204
name: artifacts-linux
204205
path: artifacts-linux
205206
L10N-Windows:
206-
if: ${{ inputs.job_to_run == 'L10N-Windows' || inputs.win_installer_link }}
207+
if: ${{ inputs.job_to_run == 'L10N-Windows' || github.event_name == 'pull_request' || inputs.win_installer_link }}
207208
runs-on: windows-latest
208209
steps:
209210
- name: Create app token
@@ -277,7 +278,7 @@ jobs:
277278
pipenv run python -c "import sys; print(sys.platform)"
278279
$env:FX_EXECUTABLE = 'C:\Program Files\Custom Firefox\firefox.exe'
279280
Start-Process -FilePath $env:FX_EXECUTABLE -ArgumentList "--version" -Wait -NoNewWindow
280-
pipenv run python check_l10n_test_cases.py
281+
pipenv run python choose_l10n_ci_set.py
281282
"0" | Set-Content -Path "TEST_EXIT_CODE"
282283
$SCRIPT_EXIT_CODE = 0
283284
Get-Content "selected_l10n_mappings" | ForEach-Object {
@@ -304,7 +305,7 @@ jobs:
304305
mv ./ci_l10n_pyproject_headed.toml ./pyproject.toml;
305306
$env:FX_EXECUTABLE = 'C:\Program Files\Custom Firefox\firefox.exe'
306307
Write-Host "FX_EXECUTABLE: $FX_EXECUTABLE"
307-
pipenv run python check_l10n_test_cases.py
308+
pipenv run python choose_l10n_ci_set.py
308309
"0" | Set-Content -Path "TEST_EXIT_CODE"
309310
$SCRIPT_EXIT_CODE = 0
310311
Get-Content "selected_l10n_mappings" | ForEach-Object {

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ drivers/
192192
temp.py
193193
tests/test_scratch.py
194194

195+
# L10N files
196+
selected_l10n_mappings
197+
195198
# Credentials
196199
credentials.json
197200
testrail_credentials.env

check_l10n_test_cases.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

choose_l10n_ci_set.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import json
2+
import os
3+
import re
4+
import sys
5+
from collections import defaultdict
6+
from subprocess import check_output
7+
8+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
9+
SLASH = "/" if "/" in SCRIPT_DIR else "\\"
10+
CI_MARK = "@pytest.mark.ci"
11+
HEADED_MARK = "@pytest.mark.headed"
12+
OUTPUT_FILE = "selected_l10n_mappings"
13+
14+
15+
def valid_l10n_mappings():
16+
"""
17+
Get a dictionary of valid l10n mappings by going through the region files.
18+
19+
Returns:
20+
The dictionary of valid l10n mappings.
21+
"""
22+
mapping = defaultdict(set)
23+
region_paths = [d for d in os.listdir("./l10n_CM/region/")]
24+
for region_path in region_paths:
25+
if region_path != "Unified.json":
26+
region = region_path.split(".")[0]
27+
with open(f"./l10n_CM/region/{region_path}", "r+") as f:
28+
region_file = json.load(f)
29+
if region_file.get("sites"):
30+
for site in region_file.get("sites"):
31+
mapping[site].add(region)
32+
return mapping
33+
34+
35+
def add_selected_mappings(mappings):
36+
"""
37+
Write the selected mappings to the output file.
38+
39+
Args:
40+
mappings (dict): A dictionary of mappings, where the keys are sites and the values are sets of regions.
41+
"""
42+
for site, regions in mappings.items():
43+
with open(OUTPUT_FILE, "a+") as f:
44+
f.write(f"{site} {' '.join(regions)}\n")
45+
46+
47+
def process_changed_file(f, selected_mappings):
48+
"""
49+
process the changed file to add the site/region mappings.
50+
51+
Args:
52+
f: the changed file.
53+
selected_mappings: the selected mappings dictionary (updated in place).
54+
"""
55+
split = f.split(SLASH)
56+
if f.startswith("l10n_CM/sites/") or f.startswith("l10n_CM/constants/"):
57+
# if constants or sites are changed, add a single site/region mapping entry.
58+
site = split[2]
59+
region = split[3]
60+
region_path = os.path.join("l10n_CM", "region", f"{region}.json")
61+
# make sure the region mapping file exists before adding the mapping
62+
if os.path.exists(region_path):
63+
selected_mappings[site].add(region)
64+
elif f.startswith("l10n_CM/region/"):
65+
# if a region file is changed, add the region to each site mapping.
66+
region = split[-1].split(".")[0]
67+
with open(f, "r+") as f:
68+
region_file = json.load(f)
69+
for site in region_file.get("sites", []):
70+
selected_mappings[site].add(region)
71+
72+
73+
if __name__ == "__main__":
74+
if os.path.exists(".env"):
75+
with open(".env") as fh:
76+
contents = fh.read()
77+
if "TESTRAIL_REPORT='true'" in contents:
78+
os.environ["TESTRAIL_REPORT"] = "true"
79+
if "RUN_ALL='true'" in contents:
80+
os.environ["MANUAL"] = "true"
81+
with open(OUTPUT_FILE, "w") as file:
82+
pass # File is created or cleared
83+
l10n_mappings = valid_l10n_mappings()
84+
if os.environ.get("TESTRAIL_REPORT") or os.environ.get("MANUAL"):
85+
# Run all tests if this is a scheduled beta or a manual run
86+
add_selected_mappings(l10n_mappings)
87+
sys.exit(0)
88+
89+
re_set_all = [
90+
r"l10n_CM/Unified/test_.*\.py",
91+
r"l10n_CM/Unified/conftest\.py",
92+
r"l10n_CM/conftest\.py",
93+
r"modules/page_object_prefs\.py",
94+
r"modules/data/about_prefs\.components\.json",
95+
r"modules/page_object_autofill\.py",
96+
r"modules/data/address_fill\.components\.json",
97+
r"modules/data/credit_card_fill\.components\.json",
98+
r"modules/browser_object_autofill_popup\.py",
99+
r"modules/data/autofill_popup\.components\.json",
100+
]
101+
re_set_select = [
102+
r"l10n_CM/constants/.*/.*/.*\.json",
103+
r"l10n_CM/sites/.*/.*/.*\.html",
104+
r"l10n_CM/region/.*\.json",
105+
]
106+
107+
re_set_all = set(
108+
[
109+
re.compile(val.replace("/", r"\\")) if SLASH == "\\" else re.compile(val)
110+
for val in re_set_all
111+
]
112+
)
113+
re_set_select = set(
114+
[
115+
re.compile(val.replace("/", r"\\")) if SLASH == "\\" else re.compile(val)
116+
for val in re_set_select
117+
]
118+
)
119+
120+
run_list = []
121+
check_output(["git", "fetch", "--quiet", "--depth=1", "origin", "main"])
122+
committed_files = (
123+
check_output(["git", "--no-pager", "diff", "--name-only", "origin/main"])
124+
.decode()
125+
.replace("/", SLASH)
126+
.splitlines()
127+
)
128+
main_conftest = "conftest.py"
129+
base_page = os.path.join("modules", "page_base.py")
130+
131+
if main_conftest in committed_files or base_page in committed_files:
132+
# Run all the tests for all mappings if main conftest or basepage changed
133+
add_selected_mappings(l10n_mappings)
134+
sys.exit(0)
135+
136+
# Run all the tests for all mappings if any core l10n model, component, conftest, or tests are changed.
137+
selected_mappings = defaultdict(set)
138+
# by default, have the demo page run for US
139+
selected_mappings["demo"] = {"US"}
140+
for f in committed_files:
141+
for re_val in re_set_all:
142+
if re_val.match(f):
143+
add_selected_mappings(l10n_mappings)
144+
sys.exit(0)
145+
# check if constants, sites or region directory files were changed or added.
146+
# if so, add the site/region mappings.
147+
for re_val in re_set_select:
148+
if re_val.match(f):
149+
process_changed_file(f, selected_mappings)
150+
151+
add_selected_mappings(selected_mappings)
152+
sys.exit(0)

l10n_CM/run_l10n.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import requests
1111

12-
from check_l10n_test_cases import valid_l10n_mappings
12+
from choose_l10n_ci_set import valid_l10n_mappings
1313

1414
current_dir = os.path.dirname(__file__)
1515
valid_flags = {"--run-headless", "-n", "--reruns", "--fx-executable", "--ci"}

modules/testrail_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import subprocess
55
import sys
66

7-
from check_l10n_test_cases import valid_l10n_mappings
7+
from choose_l10n_ci_set import valid_l10n_mappings
88
from modules import taskcluster as tc
99
from modules import testrail as tr
1010
from modules.testrail import TestRail

0 commit comments

Comments
 (0)