From 9ab2b74a05f4ff38f7a537c8d4de7cc432a3ed96 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 2 Feb 2019 05:30:59 +0800 Subject: [PATCH 1/2] test: add test processes --- .gitignore | 4 +++ .travis.yml | 15 ++++++++ ecnunetwork/__init__.py | 32 ++++++++++------- ecnunetwork/test/__init__.py | 0 ecnunetwork/test/test_ecnunetwork.py | 13 +++++++ setup.py | 52 ++++++++++++++++++++-------- tox.ini | 14 ++++++++ 7 files changed, 102 insertions(+), 28 deletions(-) create mode 100644 .travis.yml create mode 100644 ecnunetwork/test/__init__.py create mode 100644 ecnunetwork/test/test_ecnunetwork.py create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore index 7bbc71c..a9f2293 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ nosetests.xml coverage.xml *.cover .hypothesis/ +.pytest_cache # Translations *.mo @@ -99,3 +100,6 @@ ENV/ # mypy .mypy_cache/ + +# test +.testfile \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..50a4804 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +language: python +matrix: + include: + - python: 2.7 + - python: 3.5 + - python: 3.6 + - python: 3.7 + dist: xenial + sudo: true + +install: + - pip install tox-travis + +script: + - tox \ No newline at end of file diff --git a/ecnunetwork/__init__.py b/ecnunetwork/__init__.py index 5f8fb4c..f19f536 100644 --- a/ecnunetwork/__init__.py +++ b/ecnunetwork/__init__.py @@ -10,7 +10,10 @@ class ecnunetwork(object): - def __init__(self, username=None, password=None, configfile=os.path.join(os.path.expanduser('~'), ".ecnunetwork")): + def __init__( + self, username=None, password=None, configfile=os.path.join( + os.path.expanduser('~'), + ".ecnunetwork")): self.username = username self.password = password self.configfile = configfile @@ -18,31 +21,34 @@ def __init__(self, username=None, password=None, configfile=os.path.join(os.path self.flag = 0 def connect(self): - if self.username == None or self.password == None: - self.readpassword() - r = requests.post(self.url, data={ - "action": "login", "ajax": 1, "ac_id": 1, "username": self.username, "password": self.password}) + if self.username is None or self.password is None: + self._readpassword() + r = requests.post( + self.url, + data={"action": "login", "ajax": 1, "ac_id": 1, + "username": self.username, "password": self.password}) + print(r.text) if r.text.startswith("login_ok"): print("Successfully connected to the Internet.") if self.flag == 1: yorn = input("Save password? [Y/n] ") if yorn == "" or yorn == "Y" or yorn == "y" or yorn == "yes": - self.savepassword() + self._savepassword() else: yorn = input( "Fail to login. Maybe your id or password is wrong. Retry? [Y/n] ") self.flag = 2 if yorn == "" or yorn == "Y" or yorn == "y" or yorn == "yes": - self.username = None - self.password = None + self._username = None + self._password = None self.connect() - def inputpassword(self): + def _inputpassword(self): self.username = input("Please input your ECNU id: ") self.password = input("Please input your password: ") self.flag = 1 - def readpassword(self): + def _readpassword(self): if not self.flag == 2: try: with open(self.configfile, 'r') as f: @@ -51,10 +57,10 @@ def readpassword(self): self.password = config['password'] except: pass - if self.username == None or self.password == None: - self.inputpassword() + if self.username is None or self.password is None: + self._inputpassword() - def savepassword(self): + def _savepassword(self): config = {"username": self.username, "password": self.password} with open(self.configfile, 'w') as f: json.dump(config, f) diff --git a/ecnunetwork/test/__init__.py b/ecnunetwork/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ecnunetwork/test/test_ecnunetwork.py b/ecnunetwork/test/test_ecnunetwork.py new file mode 100644 index 0000000..c93c606 --- /dev/null +++ b/ecnunetwork/test/test_ecnunetwork.py @@ -0,0 +1,13 @@ +import unittest +import ecnunetwork + + +class Testecnunetwork(unittest.TestCase): + def test_loginok(self): + def answerinput(inp): + print(inp) + return 'y' + ecnunetwork.input = answerinput + n = ecnunetwork.ecnunetwork(configfile='.testfile') + n.url = 'https://php.njzjz.win/login_ok.php' + n.connect() diff --git a/setup.py b/setup.py index 7c9d671..d87a42a 100644 --- a/setup.py +++ b/setup.py @@ -1,15 +1,37 @@ -from setuptools import setup -setup(name='ecnunetwork', - description='A script to connect to Internet at East China Normal University (ECNU).', - keywords="ecnu", - url='https://github.com/njzjz/ecnunetwork', - author='Jinzhe Zeng', - author_email='jzzeng@stu.ecnu.edu.cn', - packages=['ecnunetwork'], - install_requires=['requests'], - entry_points={ - 'console_scripts': ['ecnunetwork=ecnunetwork.commandline:main', 'network=ecnunetwork.commandline:main'] - }, - setup_requires=['setuptools_scm'], - use_scm_version=True, - ) +import os + +from setuptools import find_packages, setup + +if __name__ == '__main__': + this_directory = os.path.abspath(os.path.dirname(__file__)) + with open(os.path.join(this_directory, 'docs', 'README.md'), encoding='utf-8') as f: + long_description = f.read() + setup( + name='ecnunetwork', + description='A script to connect to Internet at East China Normal University (ECNU).', + keywords="ecnu", url='https://github.com/njzjz/ecnunetwork', + author='Jinzhe Zeng', author_email='jzzeng@stu.ecnu.edu.cn', + packages=find_packages(), + install_requires=['requests'], + entry_points={ + 'console_scripts': + ['ecnunetwork=ecnunetwork.commandline:main', + 'network=ecnunetwork.commandline:main']}, + setup_requires=['setuptools_scm', 'pytest-runner'], + use_scm_version=True, + classifiers=[ + "Natural Language :: English", + "Operating System :: POSIX :: Linux", + "Operating System :: Microsoft :: Windows", + "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: Software Development :: Version Control :: Git", + ], + zip_safe=True, + long_description=long_description, + long_description_content_type='text/markdown', + tests_require=['pytest-sugar'], + ) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..391a8ae --- /dev/null +++ b/tox.ini @@ -0,0 +1,14 @@ +[tox] +envlist = py{37,36,35,27} + +[testenv] +passenv = CI TRAVIS TRAVIS_* CODECOV_* TOXENV +deps = + pytest-sugar + pytest-cov + coveralls + codecov>=1.4.0 +commands = + pytest --pyargs ecnunetwork --cov {envsitepackagesdir}/ecnunetwork -s + coveralls + codecov -e TOXENV From 8760a9018aa0ef6cf3495fe49c0c633d0043d1c5 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 2 Feb 2019 05:33:51 +0800 Subject: [PATCH 2/2] fix: fix readme errors --- README.md => docs/README.md | 0 setup.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename README.md => docs/README.md (100%) diff --git a/README.md b/docs/README.md similarity index 100% rename from README.md rename to docs/README.md diff --git a/setup.py b/setup.py index d87a42a..db192f0 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ if __name__ == '__main__': this_directory = os.path.abspath(os.path.dirname(__file__)) - with open(os.path.join(this_directory, 'docs', 'README.md'), encoding='utf-8') as f: + with open(os.path.join(this_directory, 'docs', 'README.md')) as f: long_description = f.read() setup( name='ecnunetwork',