1# Copyright © 2019 Intel Corporation 2 3# Permission is hereby granted, free of charge, to any person obtaining a copy 4# of this software and associated documentation files (the "Software"), to deal 5# in the Software without restriction, including without limitation the rights 6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7# copies of the Software, and to permit persons to whom the Software is 8# furnished to do so, subject to the following conditions: 9 10# The above copyright notice and this permission notice shall be included in 11# all copies or substantial portions of the Software. 12 13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19# SOFTWARE. 20 21from unittest import mock 22import textwrap 23 24from lxml import html 25import pytest 26 27from . import post_version 28 29 30# Mock out subprocess.run to avoid having git commits 31@mock.patch('bin.post_version.subprocess.run', mock.Mock()) 32class TestUpdateCalendar: 33 34 HEAD = textwrap.dedent("""\ 35 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 36 <html lang="en"> 37 <head> 38 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 39 <title>Release Calendar</title> 40 <link rel="stylesheet" type="text/css" href="mesa.css"> 41 </head> 42 <body> 43 """) 44 45 TABLE = textwrap.dedent("""\ 46 <table> 47 <tr> 48 <th>Branch</th> 49 <th>Expected date</th> 50 <th>Release</th> 51 <th>Release manager</th> 52 <th>Notes</th> 53 </tr> 54 """) 55 56 FOOT = "</body></html>" 57 58 TABLE_FOOT = "</table>" 59 60 def wrap_table(self, table: str) -> str: 61 return self.HEAD + self.TABLE + table + self.TABLE_FOOT + self.FOOT 62 63 def test_basic(self): 64 data = self.wrap_table(textwrap.dedent("""\ 65 <tr> 66 <td rowspan="3">19.2</td> 67 <td>2019-11-06</td> 68 <td>19.2.3</td> 69 <td>Dylan Baker</td> 70 </tr> 71 <tr> 72 <td>2019-11-20</td> 73 <td>19.2.4</td> 74 <td>Dylan Baker</td> 75 </tr> 76 <tr> 77 <td>2019-12-04</td> 78 <td>19.2.5</td> 79 <td>Dylan Baker</td> 80 <td>Last planned 19.2.x release</td> 81 </tr> 82 """)) 83 84 parsed = html.fromstring(data) 85 parsed.write = mock.Mock() 86 87 with mock.patch('bin.post_version.html.parse', 88 mock.Mock(return_value=parsed)): 89 post_version.update_calendar('19.2.3') 90 91 assert len(parsed.findall('.//tr')) == 3 92 # we need the second element becouse the first is the header 93 94 tr = parsed.findall('.//tr')[1] 95 tds = tr.findall('.//td') 96 assert tds[0].get("rowspan") == "2" 97 assert tds[0].text == "19.2" 98 assert tds[1].text == "2019-11-20" 99 100 @pytest.fixture 101 def two_releases(self) -> html.etree.ElementTree: 102 data = self.wrap_table(textwrap.dedent("""\ 103 <tr> 104 <td rowspan="1">19.1</td> 105 <td>2019-11-06</td> 106 <td>19.1.8</td> 107 <td>Not Dylan Baker</td> 108 </tr> 109 <tr> 110 <td rowspan="3">19.2</td> 111 <td>2019-11-06</td> 112 <td>19.2.3</td> 113 <td>Dylan Baker</td> 114 </tr> 115 <tr> 116 <td>2019-11-20</td> 117 <td>19.2.4</td> 118 <td>Dylan Baker</td> 119 </tr> 120 <tr> 121 <td>2019-12-04</td> 122 <td>19.2.5</td> 123 <td>Dylan Baker</td> 124 <td>Last planned 19.2.x release</td> 125 </tr> 126 """)) 127 128 p = html.fromstring(data) 129 p.write = mock.Mock() 130 return p 131 132 def test_two_releases(self, two_releases: html.etree.ElementTree): 133 with mock.patch('bin.post_version.html.parse', 134 mock.Mock(return_value=two_releases)): 135 post_version.update_calendar('19.2.3') 136 137 assert len(two_releases.findall('.//tr')) == 4 138 # we need the second element becouse the first is the header 139 140 tr = two_releases.findall('.//tr')[2] 141 tds = tr.findall('.//td') 142 assert tds[0].get("rowspan") == "2" 143 assert tds[0].text == "19.2" 144 assert tds[1].text == "2019-11-20" 145 146 def test_last_Release(self, two_releases: html.etree.ElementTree): 147 with mock.patch('bin.post_version.html.parse', 148 mock.Mock(return_value=two_releases)): 149 post_version.update_calendar('19.1.8') 150 151 assert len(two_releases.findall('.//tr')) == 4 152 # we need the second element becouse the first is the header 153 154 tr = two_releases.findall('.//tr')[1] 155 tds = tr.findall('.//td') 156 assert tds[0].get("rowspan") == "3" 157 assert tds[0].text == "19.2" 158 assert tds[1].text == "2019-11-06" 159