#reading env in tests
53 messages ยท Page 1 of 1 (latest)
it works perfectly if i run it in production
i cant use (*testing.T).Setenv bc the variables must be secret
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
the error is because a specific variable is not loading
just to make things clear, im sending an email and i need the credentials for that, which are not accessible for whatever reason
you sure? im not using relative paths and when i send an email, for example, i use the very same function to grab the data from the .env file and it works in production, even though the file is not in the same folder as the .env file
is that not then preventing it loading any of them?
if it's not in the same folder then you must be doing something funny with the current working dir, setting it to something else? there's no path on the .env you're telling it to read so it can only read from cwd..?
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
i dont believe i made anything that might change the working dir and even if i did so, why would the application work when i run the server and make a request manually?
why would they become invalid by reading them? not following ๐ฆ
i thought that the testing environment might cause something that blocks me from accessing the files
as you saw, the map containing the envs is empty
so if i tried smth like mb["API_KEY"], it would return an empty string
yeah so whats the error saying then?
which would be invalid
the error that makes the test fail?
i do think that too
gotdotenv.Read returns a map and error - you're suppressing and not checking for the error
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
if it's just tests, put something in to work outb where the file actually is
im sorry, but i didnt understand what you said
can you reformulate?
english is not my native language you know
something like godotenv.Read("../../../.env") would bodge it enough? ๐
dont know if theres a proper way to get the real repo root
i thought about doing this
and it should work, but as the application grows, it might not be the best approach
im searching for a way to get the root of the project so that it works in my machine and in case i deploy the api
something that is probably not recommended might be getting the current work dir, split it from "/src" and get only the first half
well you're not going to deploy the tests - once you compile it the .env file will presumably live with the executable?
so if the full path is like C:/Users/x/Documents/my-app/src/... it would be only C:/Users/x/Documents/my-app
im using github actions to test the application when i push into the default branch and im not sure if the GOPATH env is set there, which could be an approach
just found this which might help? unsure how the tests will look- https://go.dev/play/p/ifVRIq7Tx0
i was looking at the very same code lol
things might get a bit messy, but i think it will work
failing that hope someone who knows what they're talking about stops by ๐
gl!
setenv sets the variable only for the application
Not the rest of the system