2using System.Collections.Generic;
3using System.Diagnostics;
5using System.Text.RegularExpressions;
15 public static String gamsVersion =
null;
16 public static String gamsSystemDir =
null;
17 public static String gamsAbsoluteSystemDir =
null;
18 public static String gamsWorkingDir =
null;
19 public static String gamsAbsoluteWorkingDir =
null;
20 public static String GDX_FILE_EXTENSION =
".gdx";
22 static Logger logger = LogManager.GetCurrentClassLogger();
35 if (!Directory.Exists(dir)){
36 logger.Debug(
"Creating Dir: " + dir);
37 Directory.CreateDirectory(dir);
41 logger.Debug(
"clear: " + dir);
51 public static bool clear(String dir)
54 if (dir ==
null || !Directory.Exists(dir))
57 DirectoryInfo dirInfo =
new DirectoryInfo(dir);
59 foreach (FileInfo file
in dirInfo.GetFiles())
64 foreach (DirectoryInfo di
in dirInfo.GetDirectories())
79 if (dir !=
null && Directory.Exists(dir))
81 string[] files = Directory.GetFiles(dir);
82 foreach (
string fileName
in files)
83 File.Delete(fileName);
85 string[] subdirs = Directory.GetDirectories(dir);
86 foreach (
string subdirectory
in subdirs)
89 Directory.Delete(dir);
93 if (Directory.Exists(dir))
106 return (Directory.Exists(dir) && (Directory.GetFiles(dir,
"optgams.def").Length > 0));
116 String workingDir =
null;
120 var properties =
new Dictionary<string, string>();
123 Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
125 FileInfo dbug =
new FileInfo(filename);
126 foreach (var row
in File.ReadAllLines(filename))
128 properties.Add(row.Split(
'=')[0].Trim(),
129 normalizePath(row.Split(
'=')[1].Trim()));
132 gamsVersion = properties[
"gamsVersion"];
133 gamsSystemDir = properties[
"gamsSystemDir"];
134 workingDir = properties[
"gamsWorkingDir"];
136 String path = Path.GetFullPath(workingDir + Path.DirectorySeparatorChar + subdir);
138 gamsWorkingDir = Path.GetFullPath(path);
140 properties[
"gamsDebugLevel"],
true);
144 Debug.WriteLine(e.Message);
145 Assert.Fail(
"please check test configuration in [" + filename +
"]");
152 Assert.True(Directory.Exists(
HouseKeeper.gamsSystemDir),
153 "expect a valid gamsSystemDir being set in configuration file.");
154 HouseKeeper.gamsAbsoluteSystemDir = Path.GetFullPath(
HouseKeeper.gamsSystemDir);
159 HouseKeeper.gamsAbsoluteWorkingDir = Path.GetFullPath(
HouseKeeper.gamsWorkingDir);
162 private static string normalizePath(
string s)
164 Regex rgx =
new Regex(
@"\\*$");
165 string res = rgx.Replace(s,
"");
179 return findGAMS(Environment.GetEnvironmentVariable(
"DYLD_LIBRARY_PATH"));
188 return findGAMS(Environment.GetEnvironmentVariable(
"PATH").ToLower());
198 String GAMSDir =
null;
199 if (systemPath !=
null && Directory.Exists(systemPath))
201 String callStr =
"gams.exe";
202 foreach (var pathDir
in systemPath.Split(Path.PathSeparator))
204 if(File.Exists(pathDir + Path.DirectorySeparatorChar + callStr)){
205 GAMSDir = Path.GetFullPath(pathDir);
212 public static void delete(
string dir)
214 var pathToBeDeleted =
new DirectoryInfo(dir);
217 foreach (var file
in pathToBeDeleted.EnumerateFiles(
"*", SearchOption.AllDirectories))
221 pathToBeDeleted.Delete(
true);
223 catch (IOException e) { Console.WriteLine(e.Message); }
227 public static bool PrepareDatabaseFrom(
string library,
string model,
string workingdir)
232 List<string> arguments =
new List<string>();
233 arguments.Add(library);
234 arguments.Add(model);
236 ProcessStartInfo psi =
new ProcessStartInfo();
237 psi.FileName = arguments[0];
238 psi.Arguments =
string.Join(
" ", arguments.GetRange(1, arguments.Count - 1));
239 psi.WorkingDirectory = workingdir;
241 using (Process p = Process.Start(psi))
266 Assert.NotNull(job,
"expect a successful creation of job.");
270 Assert.Fail(
"do not expect an GAMSException to be raised.");
276 Assert.NotNull(job,
"expect a successful run.");
280 Assert.Fail(
"do not expect GAMSException when running a job.");
283 Assert.NotNull(job.
OutDB,
"do not expect an empty job output database.");
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
GAMSJob AddJobFromGamsLib(string model, GAMSCheckpoint checkpoint=null, string jobName=null)
static void prepare(String dir)
Prepare directory by checking its existence. If exists, (non - recursively) delete all its contents,...
static String findGAMSFromPathEnvironmentVariable()
Find a valid GAMS system directory from environment.
static bool clear(String dir)
(non-recursively) delete all contents under dir.
static bool rcleanup(String dir)
(recursively) delete all contents under dir, and delete dir.
static String findGAMS(String systemPath)
Find a valid GAMS system directory from the given path. A valid GAMS system directory contains a file...
static String findGAMSFromAlternativeEnvironment()
Find a valid GAMS system directory from environment. in case of Windows: read from window registry "g...
static void initializeTestFrom(String filename, String subdir)
initialize class properties from fileName and prepare directory subdir
static bool isGAMSDirectory(String dir)
check a GAMS directory.