1################################################################ 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20################################################################ 21 22"""Fake install layouts and a helper to start the fake worker.""" 23import os 24import sys 25from pathlib import Path 26 27import pytest 28 29from aootest import client 30from aootest.paths import ROOT 31 32FAKE_WORKER = Path(__file__).with_name("fake_worker.py") 33 34VERSIONRC = ("[Version]\nAllLanguages=en-US\nbuildid=510m1(Build:99000)\n%(base)sOOOPackageVersion=%(pkg)s\nProductBuildid=99000\n" 35 "ProductMajor=%(major)s\nProductMinor=%(minor)s\nRevision=7d712b94e3\n" 36 "UpdateUserAgent=<PRODUCT> (${buildid}; ${_OS}; ${_ARCH}; BundledLanguages=${AllLanguages})\n") 37 38 39def touch(path, text="x"): 40 path.parent.mkdir(parents=True, exist_ok=True) 41 path.write_text(text) 42 return path 43 44 45def make_install(root, kind, base="5.1", pkg="5.1.0", major="510", minor="1", with_base=True): 46 """A fake install of the given kind (macos, posix, windows) under `root`; returns what a user would point at.""" 47 rc = VERSIONRC % {"base": "OOOBaseVersion=%s\n" % base if with_base else "", "pkg": pkg, "major": major, "minor": minor} 48 if kind == "macos": 49 app = root / "OpenOffice5.app" 50 prog = app / "Contents" / "program" 51 touch(app / "Contents" / "MacOS" / "soffice") 52 for n in ("soffice.bin", "unopkg.bin", "pyuno.so", "uno.py", "libpyuno.dylib"): 53 touch(prog / n) 54 os.symlink("../MacOS/soffice", prog / "soffice") 55 os.symlink("unopkg.bin", prog / "unopkg") 56 touch(prog / "versionrc", rc) 57 touch(prog / "fundamentalrc", "[Bootstrap]\n") 58 return app 59 if kind == "posix": 60 top = root / "openoffice5" 61 prog = top / "program" 62 for n in ("soffice", "soffice.bin", "unopkg", "unopkg.bin", "pyuno.so", "uno.py", "libpyuno.so"): 63 touch(prog / n) 64 touch(prog / "versionrc", rc) 65 touch(prog / "fundamentalrc", "[Bootstrap]\n") 66 return top 67 top = root / "OpenOffice 5" 68 prog = top / "program" 69 for n in ("soffice.exe", "soffice.bin", "unopkg.com", "unopkg.exe", "pyuno.pyd", "uno.py"): 70 touch(prog / n) 71 touch(prog / "version.ini", rc) 72 touch(prog / "fundamental.ini", "[Bootstrap]\n") 73 return top 74 75 76@pytest.fixture 77def fake(tmp_path): 78 """fake(kind, **version) -> the path a user would point at for a fake install of that kind.""" 79 return lambda kind, **kw: make_install(tmp_path, kind, **kw) 80 81 82@pytest.fixture 83def worker_env(): 84 return dict(os.environ, PYTHONPATH=str(ROOT / "src")) 85 86 87@pytest.fixture 88def start_worker(worker_env): 89 """start_worker(mode, **client_kw) -> a started WorkerClient on the fake worker; all are closed at the end.""" 90 made = [] 91 92 def start(mode="good", **kw): 93 c = client.WorkerClient([sys.executable, str(FAKE_WORKER), mode], env=worker_env, **kw) 94 made.append(c) 95 c.start() 96 return c 97 yield start 98 for c in made: 99 c.close() 100