|
| 1 | +classdef SetEnvironmentVariableFixture < matlab.unittest.fixtures.Fixture |
| 2 | +% UsesEnvironmentVariable Fixture for setting environment variables in tests. |
| 3 | +% |
| 4 | +% This fixture reads an environment file containing key-value pairs and |
| 5 | +% sets the corresponding system environment variables prior to executing |
| 6 | +% tests. The expected format for the environment file is: |
| 7 | +% |
| 8 | +% VARIABLE_NAME=VALUE |
| 9 | +% |
| 10 | +% Lines that are empty or start with '#' (comments) are ignored. |
| 11 | +% |
| 12 | +% The fixture first attempts to load environment variables from the file |
| 13 | +% "nwbtest.env" located in the "+tests" folder. If "nwbtest.env" is not |
| 14 | +% found, it falls back to "nwbtest.default.env". When using the default file, |
| 15 | +% the fixture only applies environment variables if they are not present |
| 16 | +% in the current list of environment variables. |
| 17 | + |
| 18 | + methods |
| 19 | + function setup(fixture) %#ok<MANU> |
| 20 | + |
| 21 | + applyFromFile = true; |
| 22 | + envFilePath = fullfile(misc.getMatnwbDir, '+tests', 'nwbtest.env'); |
| 23 | + |
| 24 | + if ~isfile(envFilePath) |
| 25 | + envFilePath = fullfile(misc.getMatnwbDir, '+tests', 'nwbtest.default.env'); |
| 26 | + applyFromFile = false; |
| 27 | + end |
| 28 | + |
| 29 | + if exist("loadenv", "file") == 2 |
| 30 | + envVariables = loadenv(envFilePath); |
| 31 | + else |
| 32 | + envVariables = readEnvFile(envFilePath); |
| 33 | + end |
| 34 | + |
| 35 | + envVariableNames = string( envVariables.keys() ); |
| 36 | + if ~isrow(envVariableNames); envVariableNames = envVariableNames'; end |
| 37 | + |
| 38 | + for varName = envVariableNames |
| 39 | + varValue = envVariables(varName); |
| 40 | + if ~isenv(varName) |
| 41 | + setenv(varName, varValue) |
| 42 | + elseif applyFromFile && ~isempty(char(varValue)) |
| 43 | + setenv(varName, varValue) |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +function envMap = readEnvFile(filename) |
| 51 | +% readEnvFile Reads an environment file into a containers.Map. |
| 52 | +% |
| 53 | +% envMap = readEnvFile(filename) reads the file specified by 'filename' |
| 54 | +% and returns a containers.Map where each key is a variable name and each |
| 55 | +% value is the corresponding value from the file. |
| 56 | +% |
| 57 | +% Lines starting with '#' or empty lines are ignored. |
| 58 | + |
| 59 | + envMap = containers.Map; |
| 60 | + |
| 61 | + fileContent = fileread(filename); |
| 62 | + lines = strsplit(fileContent, newline); |
| 63 | + |
| 64 | + for i = 1:numel(lines) |
| 65 | + line = lines{i}; |
| 66 | + if isempty(line) || startsWith(line, '#') |
| 67 | + continue; |
| 68 | + end |
| 69 | + |
| 70 | + % Find the first occurrence of '=' |
| 71 | + idx = strfind(line, '='); |
| 72 | + if isempty(idx) |
| 73 | + continue; % ignore line |
| 74 | + end |
| 75 | + |
| 76 | + % Use the first '=' as the delimiter |
| 77 | + key = strtrim(line(1:idx(1)-1)); |
| 78 | + value = strtrim(line(idx(1)+1:end)); |
| 79 | + |
| 80 | + % Insert the key-value pair into the map |
| 81 | + envMap(key) = value; |
| 82 | + end |
| 83 | +end |
0 commit comments