Package: fakear-fork
FakearA module that creates fake binaries from valid configuration ( a yaml file or a dict ) and launch them in the context of your Python script, without efforts.Installationpip install fakearQuick StartThis is a mock of a fakelscommand, thatls:-args:-dexterreturn_code:0output:Omelette du FromageOn Python, you can now use Fakear to fake ls behaviour and act according to our mock.>>>fromsubprocessimportrun>>>fromfakearimportFakear>>>p=run(["ls","dexter"])ls:dexter:Nosuchfileordirectory>>>p.returncode1>>>withFakear(cfg="fake_ls.yaml"):...fake_cmd=run(["ls","dexter"])OmeletteduFromage>>>fake_cmd.returncode0You can use it as well in your unit test routine:fromfakearimportFakearfromsubprocessimportcheck_outputdeftest_faked_ls(self):withFakear(cfg="fake_ls.yaml"):proc=check_output(["ls","dexter"])assertproc.decode()=="Omelette du fromage\n"How does it works ?An instance handles a specific configuration file, with scenarios for one or
multiple commands to fake.By default, an instance is deactivated, meaning you can still configure it
before the fake happensWhen activated:The instance creates the folder that should contains the fake binaries (faked_path)It builds shell scripts corresponding to all the scenariosIt ensures every scripts are runnableIt sets in os.environ["PATH"] the faked_path at first placeSo, as long as the instance is activated, you can run faked commands with subprocess module.When deactivated, the instance removes the faked_path from PATH and deletes the folder from the filesystemDocumentationYAML FilesEvery program mock should start with the program name as a key. Then you can describe multiple behaviours for a given set of arguments.__command_name__:# Default output with no arguments-return_code:-1output:This is a fake program, please give the correct arguments# output with arguments-args:-first_arg-sec_argreturn_code:0output:This is an example of fake command# output_file with arguments-args:-first_arg-sec_argreturn_code:0output_file:semver.txt__command2_name__:# Default behaviour of a program generated by FakearYou can use those options to customise your fake program:args (Optionna̦l): a list of positionnal arguments that invoke this fake outputreturn_code: the return code when the program exitsoutput: The raw data to output when you invoke the program with these argsoutput_file: The path of a file containing the output to showNotice that if you mention no args to your list in a subcommand, it overrides the default behaviour of your fake program.Also, you have to set eitheroutputoroutput_filekeys in the same subcommand.
Otherwise, it should throw an error.APIYou can use Fakear in two ways:as an instance:>>>fakear=Fakear(cfg="path_of_a_valid.yml")>>>fakear.enable()# DO THINGS>>>fakear.disable()A Fakear instance can be manually enabled or disabled with the correct methodsas a Context Manager (recommended):withFakear(cfg="fake_ls.yml"):# DO THINGSWith the context manager, enable et disable are handled automatically, and you can set the fake path at instanciationInit variablesFakear(cfg="/path/cfg.yaml",raw=cfg_data,path="/tmp/bin")cfg: Path of the config file to useraw: Python dictpath: Path for fake binariesYou can use either cfg or raw but not both of them, as it should overlap scenarios of the same command and create undefined behaviour.Available Methodsenable() : Activate the instancedeactivate() : Deactivate the instanceset_path(path: str) : Sets a new path for faked commands. It must be absolute. You can't modify path when the instance is activatedContributeFeel free to open an issue on this repository.