| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 29 |
import logging |
| 30 |
import os |
| 31 |
import sys |
| 32 |
|
| 33 |
import helpers |
| 34 |
|
| 35 |
logging.disable(logging.CRITICAL) |
| 36 |
|
| 37 |
# Ensure we don't create .pyc files for these tests |
| 38 |
sys.dont_write_bytecode = True |
| 39 |
|
| 40 |
|
| 41 |
def test_environ_temporary_change(): |
| 42 |
"""helpers: environ() testing""" |
| 43 |
# This is a helper function; but it does enough that we want to verify |
| 44 |
# our usage of it works correctly; yes... we're testing a test |
| 45 |
|
| 46 |
e_key1 = "APPRISE_TEMP1" |
| 47 |
e_key2 = "APPRISE_TEMP2" |
| 48 |
e_key3 = "APPRISE_TEMP3" |
| 49 |
|
| 50 |
e_val1 = "ABCD" |
| 51 |
e_val2 = "DEFG" |
| 52 |
e_val3 = "HIJK" |
| 53 |
|
| 54 |
os.environ[e_key1] = e_val1 |
| 55 |
os.environ[e_key2] = e_val2 |
| 56 |
os.environ[e_key3] = e_val3 |
| 57 |
|
| 58 |
# Ensure our environment variable stuck |
| 59 |
assert e_key1 in os.environ |
| 60 |
assert e_val1 in os.environ[e_key1] |
| 61 |
assert e_key2 in os.environ |
| 62 |
assert e_val2 in os.environ[e_key2] |
| 63 |
assert e_key3 in os.environ |
| 64 |
assert e_val3 in os.environ[e_key3] |
| 65 |
|
| 66 |
with helpers.environ(e_key1, e_key3): |
| 67 |
# Eliminates Environment Variable 1 and 3 |
| 68 |
assert e_key1 not in os.environ |
| 69 |
assert e_key2 in os.environ |
| 70 |
assert e_val2 in os.environ[e_key2] |
| 71 |
assert e_key3 not in os.environ |
| 72 |
|
| 73 |
# after with is over, environment is restored to normal |
| 74 |
assert e_key1 in os.environ |
| 75 |
assert e_val1 in os.environ[e_key1] |
| 76 |
assert e_key2 in os.environ |
| 77 |
assert e_val2 in os.environ[e_key2] |
| 78 |
assert e_key3 in os.environ |
| 79 |
assert e_val3 in os.environ[e_key3] |
| 80 |
|
| 81 |
d_key = "APPRISE_NOT_SET" |
| 82 |
n_key = "APPRISE_NEW_KEY" |
| 83 |
n_val = "NEW_VAL" |
| 84 |
|
| 85 |
# Verify that our temporary variables (defined above) are not pre-existing |
| 86 |
# environemnt variables as we'll be setting them below |
| 87 |
assert n_key not in os.environ |
| 88 |
assert d_key not in os.environ |
| 89 |
|
| 90 |
# makes it easier to pass in the arguments |
| 91 |
updates = { |
| 92 |
e_key1: e_val3, |
| 93 |
e_key2: e_val1, |
| 94 |
n_key: n_val, |
| 95 |
} |
| 96 |
with helpers.environ(d_key, e_key3, **updates): |
| 97 |
# Attempt to eliminate an undefined key (silently ignored) |
| 98 |
# Eliminates Environment Variable 3 |
| 99 |
# Environment Variable 1 takes on the value of Env 3 |
| 100 |
# Environment Variable 2 takes on the value of Env 1 |
| 101 |
# Set a brand new variable that previously didn't exist |
| 102 |
assert e_key1 in os.environ |
| 103 |
assert e_val3 in os.environ[e_key1] |
| 104 |
assert e_key2 in os.environ |
| 105 |
assert e_val1 in os.environ[e_key2] |
| 106 |
assert e_key3 not in os.environ |
| 107 |
|
| 108 |
# Can't delete a variable that doesn't exist; so we're in the same |
| 109 |
# state here. |
| 110 |
assert d_key not in os.environ |
| 111 |
|
| 112 |
# Our temporary variables will be found now |
| 113 |
assert n_key in os.environ |
| 114 |
assert n_val in os.environ[n_key] |
| 115 |
|
| 116 |
# after with is over, environment is restored to normal |
| 117 |
assert e_key1 in os.environ |
| 118 |
assert e_val1 in os.environ[e_key1] |
| 119 |
assert e_key2 in os.environ |
| 120 |
assert e_val2 in os.environ[e_key2] |
| 121 |
assert e_key3 in os.environ |
| 122 |
assert e_val3 in os.environ[e_key3] |
| 123 |
|
| 124 |
# Even our temporary variables are now missing |
| 125 |
assert n_key not in os.environ |
| 126 |
assert d_key not in os.environ |