public static List <LogInfo> IniReadOp(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); CodeInfo_IniReadOp infoOp = cmd.Info.Cast <CodeInfo_IniReadOp>(); string fileName = StringEscaper.Preprocess(s, infoOp.Infos[0].FileName); Debug.Assert(fileName != null, $"{nameof(fileName)} != null"); if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg)) { return(LogInfo.LogErrorMessage(logs, errorMsg)); } IniKey[] keys = new IniKey[infoOp.Cmds.Count]; for (int i = 0; i < keys.Length; i++) { CodeInfo_IniRead info = infoOp.Infos[i]; string sectionName = StringEscaper.Preprocess(s, info.Section); string key = StringEscaper.Preprocess(s, info.Key); if (sectionName.Length == 0) { return(LogInfo.LogErrorMessage(logs, "Section name cannot be empty")); } if (key.Length == 0) { return(LogInfo.LogErrorMessage(logs, "Key name cannot be empty")); } keys[i] = new IniKey(sectionName, key); } keys = IniReadWriter.ReadKeys(fileName, keys); int successCount = 0; for (int i = 0; i < keys.Length; i++) { IniKey kv = keys[i]; CodeCommand subCmd = infoOp.Cmds[i]; if (kv.Value != null) { logs.Add(new LogInfo(LogState.Success, $"Key [{kv.Key}] and its value [{kv.Value}] successfully read", subCmd)); string escapedValue = StringEscaper.Escape(kv.Value, false, true); List <LogInfo> varLogs = Variables.SetVariable(s, infoOp.Infos[i].DestVar, escapedValue, false, false, false); LogInfo.AddCommand(varLogs, subCmd); logs.AddRange(varLogs); successCount += 1; } else { logs.Add(new LogInfo(LogState.Ignore, $"Key [{kv.Key}] does not exist", subCmd)); List <LogInfo> varLogs = Variables.SetVariable(s, infoOp.Infos[i].DestVar, string.Empty, false, false, false); logs.AddRange(varLogs); } } logs.Add(new LogInfo(LogState.Success, $"Read [{successCount}] values from [{fileName}]", cmd)); return(logs); }
public async Task UpdateScript() { string destDir = FileHelper.GetTempDir(); try { // Prepare running FileUpdater string srcScriptFile = Path.Combine(TestSetup.WebRoot, "Updater", "Standalone", "PreserveInterface_r1.script"); string workScriptFile = Path.Combine(destDir, "PreserveInterface.script"); string workScriptTreePath = Path.Combine("TestSuite", "Updater", "PreserveInterface.script"); File.Copy(srcScriptFile, workScriptFile); IniReadWriter.WriteKey(workScriptFile, "ScriptUpdate", "Url", @$ "http://localhost:{TestSetup.ServerPort}/Updater/Standalone/PreserveInterface_r2.script"); Project p = EngineTests.Project; Script sc = p.LoadScriptRuntime(workScriptFile, workScriptTreePath, new LoadScriptRuntimeOptions { AddToProjectTree = true, IgnoreMain = false, OverwriteToProjectTree = true, }); // Run an update FileUpdater updater = new FileUpdater(EngineTests.Project, null, null); (Script newScript, LogInfo log) = await updater.UpdateScriptAsync(sc, true); // Validate updated script Console.WriteLine(log); Assert.IsNotNull(newScript); Assert.IsTrue(newScript.TidyVersion.Equals("1.2", StringComparison.Ordinal)); Assert.AreEqual(SelectedState.True, newScript.Selected); IniKey[] keys = { new IniKey("Interface", "checkbox02"), new IniKey("Interface", "ComboBox02"), new IniKey("Interface", "bvl_Top"), new IniKey("Interface", "ComboBox01"), new IniKey("Interface", "CheckBox01"), new IniKey("Interface", "Button01"), new IniKey("Interface", "CheckBox03"), new IniKey("ThirdInterface", "RadioGroup01"), new IniKey("FourthInterface", "RadioButton01"), new IniKey("FourthInterface", "RadioButton02"), new IniKey("FourthInterface", "RadioButton03"), }; keys = IniReadWriter.ReadKeys(newScript.RealPath, keys); Dictionary <string, string> ifaceDict = keys.Where(x => x.Section.Equals("Interface")) .ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase); Assert.IsTrue(ifaceDict["checkbox02"].Equals("checkbox02,1,3,10,10,200,18,False", StringComparison.Ordinal)); Assert.IsTrue(ifaceDict["ComboBox02"].Equals("C,1,4,262,120,150,22,A,B,C", StringComparison.Ordinal)); Assert.IsTrue(ifaceDict["bvl_Top"].Equals("\"Updated\",1,12,254,101,228,70,8,Normal", StringComparison.Ordinal)); Assert.IsTrue(ifaceDict["ComboBox01"].Equals("A,1,4,42,66,150,21,A,B,C", StringComparison.Ordinal)); Assert.IsTrue(ifaceDict["CheckBox01"].Equals("CheckBox01,1,3,42,98,173,18,False", StringComparison.Ordinal)); Assert.IsTrue(ifaceDict["Button01"].Equals("Button01,1,8,262,46,80,25,TestSection,0,False", StringComparison.Ordinal)); Assert.IsTrue(ifaceDict["CheckBox03"].Equals("CheckBox03,1,3,100,400,200,18,True", StringComparison.Ordinal)); ifaceDict = keys.Where(x => x.Section.Equals("ThirdInterface")) .ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase); Assert.IsTrue(ifaceDict["RadioGroup01"].Equals("RadioGroup01,1,14,10,30,150,60,A,B,C,1", StringComparison.Ordinal)); ifaceDict = keys.Where(x => x.Section.Equals("FourthInterface")) .ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase); Assert.IsTrue(ifaceDict["RadioButton01"].Equals("A,1,11,10,30,120,20,False", StringComparison.Ordinal)); Assert.IsTrue(ifaceDict["RadioButton02"].Equals("B,1,11,10,50,120,20,False", StringComparison.Ordinal)); Assert.IsTrue(ifaceDict["RadioButton03"].Equals("C,1,11,10,70,120,20,True", StringComparison.Ordinal)); Assert.IsFalse(IniReadWriter.ContainsSection(newScript.RealPath, "SecondInterface")); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }