windows - How to shadow python builtin pwd module -


there python code works under linux. uses pwd module in way that:

import pwd    ...   def func():       user=pwd.getpwnam(user)[2] 

now have specific need cover code tests, , tests have runnable under windows. program intended run under linux. problem pwd module not available under windows, code under test fail importerror, if implementation of pwd functions mocked using magicmock.

the basic idea solve issue shadow pwd module when running tests. when running tests, stub shadow pwd , when running main program, original (unix) pwd used. created such stub @ test pythonpath:

# pwd.py def getpwnam(user):     print("yessssssss") 

but not seem shadow pwd module, in debugger see built-in pwd imported. i'm java developer, i'm sorry if way of doing things not "pythonic". ideas welcome.

rename pwd.py else, such winpwd.py. use:

try:     import pwd except importerror:     import winpwd pwd 

by importing pwd way, built-in pwd on linux, , winpwd on windows. should able run tests , mock please.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -