#reading env in tests

53 messages ยท Page 1 of 1 (latest)

fiery rock
#

i have some env in a .env file but if i try to read them when i run a test, it just dont read the variables, even though the file exists

#

it works perfectly if i run it in production

#

i cant use (*testing.T).Setenv bc the variables must be secret

fossil matrix
#

you're skipping the error, might want to check if somethings there?

#

might be a path issue - it doesn't look like you're running it from the same folder that file is in

fiery rock
#

just to make things clear, im sending an email and i need the credentials for that, which are not accessible for whatever reason

fiery rock
fossil matrix
fossil matrix
fiery rock
# fossil matrix is that not then preventing it loading any of them?

i think that what i said was a bit confusing, lemme reformulate: the error is because i have to assign the credentials (which are in the .env file) to send the email. However, if i try to read the file and load de envs using godotenv.Read, there credentials become invalid

#

and thats the cause of the error

fiery rock
fossil matrix
#

why would they become invalid by reading them? not following ๐Ÿ˜ฆ

fiery rock
#

i thought that the testing environment might cause something that blocks me from accessing the files

fiery rock
#

so if i tried smth like mb["API_KEY"], it would return an empty string

fossil matrix
#

yeah so whats the error saying then?

fiery rock
#

which would be invalid

fossil matrix
#

on the godotenv.Read line

#

we might be talking cross purposes ๐Ÿ˜„

fiery rock
fiery rock
fossil matrix
#

gotdotenv.Read returns a map and error - you're suppressing and not checking for the error

fiery rock
#

thats true

#

lemme see

#

its pretty much what you said

#

open .env: The system cannot find the file specified.

#

but how can i solve it?

#

just so that you have a better vision over whats going on, the test is located in the create_test.go file

fossil matrix
#

if it's just tests, put something in to work outb where the file actually is

fiery rock
#

can you reformulate?

#

english is not my native language you know

fossil matrix
#

something like godotenv.Read("../../../.env") would bodge it enough? ๐Ÿ™‚

#

dont know if theres a proper way to get the real repo root

fiery rock
#

and it should work, but as the application grows, it might not be the best approach

fiery rock
#

something that is probably not recommended might be getting the current work dir, split it from "/src" and get only the first half

fossil matrix
#

well you're not going to deploy the tests - once you compile it the .env file will presumably live with the executable?

fiery rock
#

so if the full path is like C:/Users/x/Documents/my-app/src/... it would be only C:/Users/x/Documents/my-app

fiery rock
fossil matrix
fiery rock
#

i was looking at the very same code lol

#

things might get a bit messy, but i think it will work

fossil matrix
#

failing that hope someone who knows what they're talking about stops by ๐Ÿ˜„

fiery rock
#

it worked as i expected

#

thank you

#

now i just need to try it in github actions

fossil matrix
#

gl!

coarse mesa
#

Not the rest of the system