Files
yiyezhqiu-refactored/tests/vbs/test_refdes_bridge.vbs
T

257 lines
8.5 KiB
Plaintext

Option Explicit
Dim Fso
Dim ScriptRoot
Dim IntegrationRoot
Dim TempRoot
Dim ObjectMap
Dim OriginalsMap
Dim Component
Dim State
Dim DxToolsHome
Class FakeAttribute
Private CurrentValue
Private CurrentVisible
Public Property Get Value()
Value = CurrentValue
End Property
Public Property Let Value(newValue)
If CStr(newValue) = CStr(FailValue) Then
Err.Raise vbObjectError + 901, "FakeAttribute", "injected failure"
End If
CurrentValue = CStr(newValue)
End Property
Public Property Get Visible()
Visible = CurrentVisible
End Property
Public Property Let Visible(newValue)
CurrentVisible = newValue
End Property
Public Sub Initialize(value, visible)
CurrentValue = CStr(value)
CurrentVisible = visible
FailValue = Chr(0)
End Sub
Public FailValue
End Class
Class FakeAttributes
Private Items
Public Sub Initialize()
Set Items = CreateObject("Scripting.Dictionary")
End Sub
Public Sub AddAttribute(name, value, visible)
Dim attribute
Set attribute = New FakeAttribute
Call attribute.Initialize(value, visible)
If Items.Exists(LCase(CStr(name))) Then
Items.Remove LCase(CStr(name))
End If
Items.Add LCase(CStr(name)), attribute
End Sub
Public Property Get Item(name)
If Not Items.Exists(LCase(CStr(name))) Then
Err.Raise vbObjectError + 902, "FakeAttributes", _
"attribute not found: " & CStr(name)
End If
Set Item = Items(LCase(CStr(name)))
End Property
End Class
Class FakeComponent
Private CurrentRefdes
Public FailTarget
Public Attributes
Public Property Get Refdes()
Refdes = CurrentRefdes
End Property
Public Property Let Refdes(value)
If CStr(value) = CStr(FailTarget) Then
Err.Raise vbObjectError + 901, "FakeComponent", "injected failure"
End If
CurrentRefdes = CStr(value)
End Property
Public Sub Initialize(value)
CurrentRefdes = CStr(value)
FailTarget = Chr(0)
Set Attributes = New FakeAttributes
Call Attributes.Initialize()
End Sub
End Class
Set Fso = CreateObject("Scripting.FileSystemObject")
ScriptRoot = Fso.GetParentFolderName(WScript.ScriptFullName)
IntegrationRoot = Fso.GetAbsolutePathName(Fso.BuildPath( _
ScriptRoot, "..\..\integration\dxdesigner795"))
Call LoadGlobal(Fso.BuildPath(IntegrationRoot, "BridgeCommon.vbs"))
Call LoadGlobal(Fso.BuildPath(IntegrationRoot, "core\Transaction.vbs"))
Call LoadGlobal(Fso.BuildPath(IntegrationRoot, "core\Mailbox.vbs"))
TempRoot = Fso.BuildPath(Fso.GetSpecialFolder(2), _
"dx-tools-bridge-" & Replace(CStr(Timer), ".", "-"))
Fso.CreateFolder TempRoot
DxToolsHome = TempRoot & "\Tool Root"
Call AssertEqual(DxBridgeUiCommand(TempRoot, "bom"), _
DxToolsQuote(DxToolsHome & "\DxDesignerTools.exe") & _
" ui --request " & DxToolsQuote(TempRoot & "\request.json") & _
" --feature " & DxToolsQuote("bom"), "ui command with feature")
Call AssertEqual(DxBridgeUiCommand(TempRoot, ""), _
DxToolsQuote(DxToolsHome & "\DxDesignerTools.exe") & _
" ui --request " & DxToolsQuote(TempRoot & "\request.json"), _
"ui command without feature")
Set ObjectMap = CreateObject("Scripting.Dictionary")
Set OriginalsMap = CreateObject("Scripting.Dictionary")
Set Component = New FakeComponent
Component.Initialize "R?"
ObjectMap.Add "c000001", Component
Call WriteTransaction(TempRoot, "refdes", "", "R?", "R1")
State = DxProcessApply(TempRoot, "run-vbs", "snapshot-vbs", _
ObjectMap, OriginalsMap)
Call AssertEqual(State, "applied", "successful state")
Call AssertEqual(Component.Refdes, "R1", "successful refdes")
Call AssertContains(DxToolsReadUtf8(Fso.BuildPath( _
TempRoot, "apply-status.json")), """state"": ""applied""", _
"successful status")
Component.Initialize "R?"
Component.FailTarget = "R9"
Call WriteTransaction(TempRoot, "refdes", "", "R?", "R9")
State = DxProcessApply(TempRoot, "run-vbs", "snapshot-vbs", _
ObjectMap, OriginalsMap)
Call AssertEqual(State, "rolled-back", "rollback state")
Call AssertEqual(Component.Refdes, "R?", "rollback refdes")
Component.Initialize "R8"
Component.FailTarget = Chr(0)
Call WriteTransaction(TempRoot, "refdes", "", "R?", "R2")
State = DxProcessApply(TempRoot, "run-vbs", "snapshot-vbs", _
ObjectMap, OriginalsMap)
Call AssertEqual(State, "rejected", "stale state")
Call AssertEqual(Component.Refdes, "R8", "stale refdes unchanged")
Component.Initialize "R8"
Component.Attributes.AddAttribute "Value", "10K", "1"
Call WriteTransaction(TempRoot, "property", "Value", "10K", "47K")
State = DxProcessApply(TempRoot, "run-vbs", "snapshot-vbs", _
ObjectMap, OriginalsMap)
Call AssertEqual(State, "applied", "property state")
Call AssertEqual(Component.Attributes.Item("Value").Value, "47K", _
"property value written")
Component.Initialize "R8"
Component.Attributes.AddAttribute "Part Name", "RES", "1"
Call WriteTransaction(TempRoot, "visibility", "Part Name", "1", "0")
State = DxProcessApply(TempRoot, "run-vbs", "snapshot-vbs", _
ObjectMap, OriginalsMap)
Call AssertEqual(State, "applied", "visibility state")
Call AssertEqual(DxBridgeNormalizeVisible( _
Component.Attributes.Item("Part Name").Visible), "0", _
"visibility written")
Component.Initialize "R8"
Component.Attributes.AddAttribute "Missing", "x", "1"
Call WriteTransaction(TempRoot, "property", "Absent", "x", "y")
State = DxProcessApply(TempRoot, "run-vbs", "snapshot-vbs", _
ObjectMap, OriginalsMap)
Call AssertEqual(State, "rejected", "missing attribute rejected")
Call TestClaimAndCleanup
Fso.DeleteFolder TempRoot, True
WScript.Echo "DesignBridge transaction assertions passed"
Sub TestClaimAndCleanup
Dim runsRoot
Dim index
Dim folderPath
Dim sourcePath
Dim claimPath
runsRoot = Fso.BuildPath(TempRoot, "runs")
Fso.CreateFolder runsRoot
For index = 1 To 25
folderPath = Fso.BuildPath(runsRoot, "run-" & _
Right("00" & CStr(index), 3))
Fso.CreateFolder folderPath
Next
Call DxBridgeCleanupRuns(runsRoot, "run-025")
Call AssertEqual(Fso.GetFolder(runsRoot).SubFolders.Count, 20, _
"run cleanup keeps 20")
Call AssertEqual(Fso.FolderExists(Fso.BuildPath(runsRoot, "run-025")), _
True, "run cleanup keeps current")
sourcePath = Fso.BuildPath(TempRoot, "claim-source.tsv")
claimPath = Fso.BuildPath(TempRoot, "claim-target.tsv")
Call Fso.CreateTextFile(sourcePath, True, False).Close
Call AssertEqual(DxBridgeClaimFile(Fso, sourcePath, claimPath), _
True, "claim moves source")
Call AssertEqual(Fso.FileExists(sourcePath), False, "claim removes source")
Call AssertEqual(Fso.FileExists(claimPath), True, "claim creates target")
End Sub
Sub WriteTransaction(root, op, propertyName, expectedValue, newValue)
Dim resultPath
Dim backupPath
Dim requestPath
Dim resultStream
Dim requestText
resultPath = Fso.BuildPath(root, "result.tsv")
backupPath = Fso.BuildPath(root, "backup.json")
requestPath = Fso.BuildPath(root, "apply-request.json")
Set resultStream = Fso.CreateTextFile(resultPath, True, False)
resultStream.WriteLine "1" & vbTab & op & vbTab & "c000001" & vbTab & _
propertyName & vbTab & expectedValue & vbTab & newValue & vbTab & _
newValue
resultStream.Close
Call Fso.CreateTextFile(backupPath, True, False).Close
requestText = "{" & vbCrLf & _
" ""format_version"": 2," & vbCrLf & _
" ""run_id"": ""run-vbs""," & vbCrLf & _
" ""snapshot_id"": ""snapshot-vbs""," & vbCrLf & _
" ""backup_path"": """ & DxToolsJson(backupPath) & """," & vbCrLf & _
" ""schedule_sha256"": """ & DxToolsSha256File(resultPath) & _
"""" & vbCrLf & "}" & vbCrLf
Call DxToolsWriteUtf8Atomic(requestPath, requestText)
End Sub
Sub LoadGlobal(path)
Dim stream
Dim text
Set stream = Fso.OpenTextFile(path, 1, False, 0)
text = stream.ReadAll
stream.Close
ExecuteGlobal text
End Sub
Sub AssertEqual(actualValue, expectedValue, label)
If CStr(actualValue) <> CStr(expectedValue) Then
Call Fail(label & ": expected [" & CStr(expectedValue) & _
"] got [" & CStr(actualValue) & "]")
End If
End Sub
Sub AssertContains(actualValue, expectedValue, label)
If InStr(CStr(actualValue), CStr(expectedValue)) = 0 Then
Call Fail(label & ": missing [" & CStr(expectedValue) & "]")
End If
End Sub
Sub Fail(message)
WScript.Echo "FAIL: " & message
WScript.Quit 1
End Sub