Repository: kaizendorks/pymongo_inmemory (language: python)

Issue:
Fix the linting issue poping up in python standard library
There is a linting issue caused by a Python 3.6 std lib  (smtp) which should be ignored by the linter.

https://github.com/kaizendorks/pymongo_inmemory/runs/853863869




Discussion / hints:

Fix patch:
diff --git a/.github/workflows/pypi-publish.yml b/.github/workflows/pypi-publish.yml
index 89e21d7..9204837 100644
--- a/.github/workflows/pypi-publish.yml
+++ b/.github/workflows/pypi-publish.yml
@@ -14,9 +14,10 @@ jobs:
       with:
         python-version: 3.6.10
     - name: Install poetry
-      uses: dschep/install-poetry-action@v1.3
+      uses: snok/install-poetry@v1.1.1
       with:
-        version: 1.0.5
+        virtualenvs-create: true
+        virtualenvs-in-project: true
     - name: Install dependencies
       run: |
         poetry install
diff --git a/.vscode/launch.json b/.vscode/launch.json
index f6ed3c6..97fcec9 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -27,7 +27,7 @@
             "type": "python",
             "request": "launch",
             "stopOnEntry": false,
-            "pythonPath": "${command:python.interpreterPath}",
+            "python": "${command:python.interpreterPath}",
             "module": "pytest",
             "args": [
                 "-sv"
diff --git a/README.md b/README.md
index da80162..bcd5fd0 100644
--- a/README.md
+++ b/README.md
@@ -9,16 +9,33 @@ A mongo mocking library with an ephemeral MongoDB running in memory.
 pip install pymongo-inmemory
 ```
 
+## Configuration
+| Config param     | Description                                                          | Optional? | Default                                          |
+|------------------|----------------------------------------------------------------------|-----------|--------------------------------------------------|
+| mongo_version    | Which MongoD version to download and use.                            | Yes       | 4.0.10                                           |
+| mongod_port      | Override port preference.                                            | Yes       | Automatically determined between 27017 and 28000 |
+| operating_system | This makes sense for Linux setting, where there are several flavours | Yes       | Automatically determined (Generic for Linux)     |
+
+
 ## Usage
-Insert a new section to your project's `setup.cfg` for the operating system and mongo
+### Configure
+There are several ways you can configure `pymongo_inmemory`.
+
+1. Insert a new section to your project's `setup.cfg` for the operating system and mongo
 version you want to spin up:
-```ini
-[pymongo_inmemory]
-mongo_version = 4.0
-operating_system = osx
-```
+    ```ini
+    [pymongo_inmemory]
+    mongo_version = 4.0.10
+    mongod_port = 27019
+    ```
+2. Define an ALL_CAPS environment variable with prefix `PYMONGOIM__` (attention to trailing double
+   underscores.) For instance, to override the port, set up an environment variable
+   `PYMONGOIM__MONGOD_PORT`.
+
+### Import and use
+`pymongo_inmemory` wraps the client that comes from `pymongo` and configures and ephemeral server.
+Then you can import `MongoClient` from `pymongo_inmemory` instead of `pymongo` and use it:
 
-then use the `pymongo_inmemory` client instead of original one:
 ```python
 from pymongo_inmemory import MongoClient
 
@@ -33,7 +50,7 @@ with MongoClient() as client:
     # do stuff
 ```
 
-## Supported Python version
+## Supported Python versions
 Since `pytest` uses [`LocalPath`](https://py.readthedocs.io/en/latest/path.html) for path related
 operations and on python versions older than 3.6 `LocalPath` does not behave well with all path
 related operations, we are setting **Python 3.6.10** in our development.
diff --git a/pymongo_inmemory/mongod.py b/pymongo_inmemory/mongod.py
index 784aff9..2ce0f7d 100644
--- a/pymongo_inmemory/mongod.py
+++ b/pymongo_inmemory/mongod.py
@@ -11,7 +11,7 @@
 import subprocess
 from tempfile import TemporaryDirectory
 
-from ._utils import find_open_port
+from ._utils import find_open_port, conf
 from .downloader import bin_folder, download
 
 logger = logging.getLogger("PYMONGOIM_MONGOD")
@@ -44,7 +44,12 @@ def __init__(self):
 
     @property
     def port(self):
-        return str(find_open_port(range(27017, 28000)))
+        set_port = conf('mongod_port')
+        if set_port is None:
+            return str(find_open_port(range(27017, 28000)))
+        else:
+            logger.warn("Using Mongod port set by user: {}".format(set_port))
+            return set_port
 
 
 class Mongod:
diff --git a/setup.cfg b/setup.cfg
index e433143..bf8c8da 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,5 +1,6 @@
 [flake8]
 max-line-length = 89
+exclude = .git,__pycache__,.venv
 
 [tool:pytest]
 python_paths = .


Test patch:
diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml
index 3dae537..31cd720 100644
--- a/.github/workflows/testing.yml
+++ b/.github/workflows/testing.yml
@@ -12,9 +12,10 @@ jobs:
       with:
         python-version: 3.6.10
     - name: Install poetry
-      uses: dschep/install-poetry-action@v1.3
+      uses: snok/install-poetry@v1.1.1
       with:
-        version: 1.0.5
+        virtualenvs-create: true
+        virtualenvs-in-project: true
     - name: Install dependencies
       run: |
         poetry install
diff --git a/tests/integrity/test_integrity.sh b/tests/integrity/test_integrity.sh
index 791478e..8264a58 100644
--- a/tests/integrity/test_integrity.sh
+++ b/tests/integrity/test_integrity.sh
@@ -1,13 +1,18 @@
 #!/bin/bash
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
-# echo $DIR
-bash $DIR/test_mongod_cleanup.sh
 
+bash $DIR/test_mongod_cleanup.sh
 RESULT=$?
 if [ $RESULT == 0 ]; then
-  echo "All integrity tests successful."
-  exit 0
-else
-  echo "Some tests failed."
-  exit 1
+  bash $DIR/test_mongod_port_override.sh
+  RESULT=$?
+  if [ $RESULT == 0 ]; then
+    echo "============================================================================================="
+    echo "All integrity tests successful."
+    exit 0
+  fi
 fi
+
+echo "============================================================================================="
+echo "Some tests failed."
+exit 1
diff --git a/tests/integrity/test_mongod_cleanup.sh b/tests/integrity/test_mongod_cleanup.sh
index d583072..514d4d1 100644
--- a/tests/integrity/test_mongod_cleanup.sh
+++ b/tests/integrity/test_mongod_cleanup.sh
@@ -5,9 +5,12 @@
 # This test checks if the main python process can properly clean up after being
 # terminated.
 
-echo "Starting clean up test."
+echo "============================================================================================="
+echo "  Starting clean up test."
+echo "---------------------------------------------------------------------------------------------"
 
 WAIT_TO_KILL=30
+WAIT_FOR_SPINDOWN=2
 
 MONGOD_BEFORE=$(pgrep mongod)
 echo "All mongod processes running before we kick off: $MONGOD_BEFORE"
@@ -18,10 +21,13 @@ echo "Started python process with PID $PYTHON_PROCESS_ID. Waiting to kill in $WA
 sleep $WAIT_TO_KILL
 echo "Killing python process, $PYTHON_PROCESS_ID"
 kill $PYTHON_PROCESS_ID
+echo "Waiting, $WAIT_FOR_SPINDOWN, to allow shutoff"
+sleep $WAIT_FOR_SPINDOWN
 
 MONGOD_AFTER=$(pgrep mongod)
 echo "All mongod processes running after we kill main thread: $MONGOD_AFTER"
 
+echo "---------------------------------------------------------------------------------------------"
 if [ "$MONGOD_BEFORE" == "$MONGOD_AFTER" ]; then
   echo "Clean up test successful."
   exit 0
diff --git a/tests/integrity/test_mongod_port_override.sh b/tests/integrity/test_mongod_port_override.sh
new file mode 100644
index 0000000..29c9eb5
--- /dev/null
+++ b/tests/integrity/test_mongod_port_override.sh
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# test_mongod_port_override.sh
+#
+# This test checks if we can override cound mongod port with an environmental variable.
+
+echo "============================================================================================="
+echo "  Starting port override test."
+echo "---------------------------------------------------------------------------------------------"
+
+export PYMONGOIM__MONGOD_PORT=27019
+
+WAIT_FOR_SPINUP=10
+WAIT_FOR_SPINDOWN=2
+
+MONGOD_BEFORE=$(pgrep mongod)
+echo "All mongod processes running before we kick off: $MONGOD_BEFORE"
+
+$(which python3) -m pymongo_inmemory.mongod > /dev/null &
+PYTHON_PROCESS_ID=$!
+echo "Started python process with PID $PYTHON_PROCESS_ID. Waiting $WAIT_FOR_SPINUP seconds to allow bootup."
+sleep $WAIT_FOR_SPINUP
+echo "Checking bound port"
+lsof -iTCP -sTCP:LISTEN | grep mongod | grep $PYMONGOIM__MONGOD_PORT
+RESULT=$?
+echo "Killing python process, $PYTHON_PROCESS_ID"
+kill $PYTHON_PROCESS_ID
+echo "Waiting, $WAIT_FOR_SPINDOWN, to allow shutoff"
+sleep $WAIT_FOR_SPINDOWN
+unset PYMONGOIM__MONGOD_PORT
+
+MONGOD_AFTER=$(pgrep mongod)
+echo "All mongod processes running after we kill main thread: $MONGOD_AFTER"
+
+echo "---------------------------------------------------------------------------------------------"
+if [ $RESULT == 1 ]; then
+  echo "Port override failed!"
+  exit 1
+else
+  echo "Port override successful"
+  exit 0
+fi