|
| 1 | +# Copyright 2017 Christoph Reiter |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 4 | +# a copy of this software and associated documentation files (the |
| 5 | +# "Software"), to deal in the Software without restriction, including |
| 6 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 8 | +# permit persons to whom the Software is furnished to do so, subject to |
| 9 | +# the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included |
| 12 | +# in all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 | +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 19 | +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 20 | +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +import os |
| 23 | +import sys |
| 24 | + |
| 25 | +from hypothesis.strategies import ( |
| 26 | + composite, |
| 27 | + sampled_from, |
| 28 | + lists, |
| 29 | + integers, |
| 30 | + binary, |
| 31 | + randoms, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +@composite |
| 36 | +def fspaths(draw, max_size: int | None = None) -> str: |
| 37 | + """A hypothesis strategy which gives valid path values. |
| 38 | +
|
| 39 | + Valid path values are everything which when passed to open() will not raise |
| 40 | + ValueError or TypeError (but might raise OSError due to file system or |
| 41 | + operating system restrictions). |
| 42 | + """ |
| 43 | + |
| 44 | + if os.name == "nt": |
| 45 | + hight_surrogate = integers(min_value=0xD800, max_value=0xDBFF).map( |
| 46 | + lambda i: chr(i) |
| 47 | + ) |
| 48 | + low_surrogate = integers(min_value=0xDC00, max_value=0xDFFF).map( |
| 49 | + lambda i: chr(i) |
| 50 | + ) |
| 51 | + uni_char = integers(min_value=1, max_value=sys.maxunicode).map(lambda i: chr(i)) |
| 52 | + any_char = sampled_from( |
| 53 | + [draw(uni_char), draw(hight_surrogate), draw(low_surrogate)] |
| 54 | + ) |
| 55 | + any_text = lists(any_char, max_size=max_size).map(lambda l: "".join(l)) |
| 56 | + path_text = any_text |
| 57 | + else: |
| 58 | + unix_path_bytes = binary(max_size=max_size).map(lambda b: b.replace(b"\x00", b" ")) |
| 59 | + path_text = unix_path_bytes.map( |
| 60 | + lambda b: b.decode(sys.getfilesystemencoding(), "surrogateescape") |
| 61 | + ) |
| 62 | + r = draw(randoms()) |
| 63 | + |
| 64 | + def shuffle_text(t): |
| 65 | + l = list(t) |
| 66 | + r.shuffle(l) |
| 67 | + return "".join(l) |
| 68 | + |
| 69 | + path_text = path_text.map(shuffle_text) |
| 70 | + |
| 71 | + return draw(path_text) |
0 commit comments