go - Mocking crypto/ssh/terminal -


has had success or have ideas on best way mock entry (for testing purposes) term.readpassword(int(os.stdin.fd())) call in golang.org/x/crypto/ssh/terminal package?

i have tried creating temp file (vs os.stdin) , writing string values testing\n or testing\r temp file error inappropriate ioctl device. i'm guessing tty related or specific format missing(?) not sure.

help appreciated.

if stubbing test creating fake file os.stdin referencing, tests become tremendously os specific when try handle readpassword(). because under hood go compiling separate syscalls depending on os. readpassword() implemented here, syscalls based on architecture , os in this directory. can see there many. cannot think of way stub test in way specifying.

with limited understanding of problem solution propose inject simple interface along lines of:

type passwordreader interface {     readpassword(fd int) ([]byte, error) }  func (pr passwordreader) readpassword(fd int) ([]byte, error) {     return terminal.readpassword(fd) } 

this way can pass in fake object tests, , stub response readpassword. know feels writing code tests, can reframe thought terminal outside dependency (i/o) should injected! tests not ensuring code works, helping make design decisions.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -