import * as assert from 'assert' ; import { commands } from 'vscode' ; import { IDeploymentParametersFile , IDeploymentTemplate } from "../support/diagnostics" ; import { mapParameterFile } from '../support/mapParameterFile' ; import { getDocumentMarkers , removeEOLMarker } from "../support/parseTemplate" ; import { stringify } from '../support/stringify' ; import { TempDocument , TempEditor , TempFile } from '../support/TempFile' ; import { testWithLanguageServer } from '../support/testWithLanguageServer' ; const longTemplate = { "$schema" : "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#" , "contentVersion" : "1.0.0.0" , "parameters" : { "requiredString" : { "type" : "string" } , "requiredArray" : { "type" : "int" } , "optionalObject" : { "type" : "int" , "defaultValue" : { "abc" : "def" } } , "optionalInt" : { "type" : "int" , "defaultValue" : 1 } } } ; const templateWithOneOptionalParam = { "$schema" : "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#" , "contentVersion" : "1.0.0.0" , "parameters" : { "optionalInt" : { "type" : "int" , "defaultValue" : 1 } } } ; suite ( "Add missing parameters - functional" , ( ) => { enum Params { "all" , "onlyRequired" } function createAddMissingParamsTestAllAndRequired ( testName , params , template , expectedResultForAllParameters , expectedResultForOnlyRequiredParameters ) { createAddMissingParamsTest ( `${ testName } - all Parameters` , params , template , Params . all , expectedResultForAllParameters ) ; createAddMissingParamsTest ( `${ testName } - only required parameters` , params , template , Params . onlyRequired , expectedResultForOnlyRequiredParameters ) ; } function createAddMissingParamsTest ( testName , params , template , whichParams , expectedResult ) { testWithLanguageServer ( testName , async ( ) => { let editor ; let templateFile ; try { const { unmarkedText } = getDocumentMarkers ( params ) ; expectedResult = removeEOLMarker ( expectedResult ) ; if ( template ) { templateFile = TempFile . fromContents ( stringify ( template , 4 ) , 'template' ) ; } let paramsFile = TempFile . fromContents ( unmarkedText , 'params' ) ; if ( templateFile ) { await mapParameterFile ( templateFile . uri , paramsFile . uri ) ; } const paramsDoc = new TempDocument ( paramsFile ) ; editor = new TempEditor ( paramsDoc ) ; await editor . open ( ) ; await commands . executeCommand ( whichParams === Params . all ? 'azurerm-vscode-tools.codeAction.addAllMissingParameters' : 'azurerm-vscode-tools.codeAction.addMissingRequiredParameters' ) ; const actualResult = paramsDoc . realDocument . getText ( ) ; assert . equal ( actualResult , expectedResult ) ; } finally { if ( editor ) { await editor . dispose ( ) ; } if ( templateFile ) { await mapParameterFile ( templateFile . uri , undefined , false ) ; } } } ) ; } createAddMissingParamsTestAllAndRequired ( "Empty parameters section" , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    }
}` , longTemplate , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "requiredString": {
            "value": "" // TODO: Fill in parameter value
        },
        "requiredArray": {
            "value": 0 // TODO: Fill in parameter value
        },
        "optionalObject": {
            "value": {
                "abc": "def"
            }
        },
        "optionalInt": {
            "value": 1
        }
    }
}` , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "requiredString": {
            "value": "" // TODO: Fill in parameter value
        },
        "requiredArray": {
            "value": 0 // TODO: Fill in parameter value
        }
    }
}` ) ; createAddMissingParamsTestAllAndRequired ( "Empty parameters section with no whitespace" , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {}
}` , templateWithOneOptionalParam , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "optionalInt": {
            "value": 1
        }
    }
}` , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {}
}` ) ; createAddMissingParamsTestAllAndRequired ( "Comma after existing param" , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "unknownParameter": {
            "value": 1
        }
    }
}` , templateWithOneOptionalParam , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "unknownParameter": {
            "value": 1
        },
        "optionalInt": {
            "value": 1
        }
    }
}` , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "unknownParameter": {
            "value": 1
        }
    }
}` ) ; createAddMissingParamsTestAllAndRequired ( "Insert after existing param and comments" , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "existingParam1": {
            "value": 1
        }
        // Hello
    }
}` , templateWithOneOptionalParam , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "existingParam1": {
            "value": 1
        },
        // Hello
        "optionalInt": {
            "value": 1
        }
    }
}` , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "existingParam1": {
            "value": 1
        }
        // Hello
    }
}` ) ; createAddMissingParamsTestAllAndRequired ( "Insert after comments only" , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        // Hello
    }
}` , templateWithOneOptionalParam , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        // Hello
        "optionalInt": {
            "value": 1
        }
    }
}` , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        // Hello
    }
}` ) ; createAddMissingParamsTestAllAndRequired ( "Some params aren't missing" , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "requiredArray": {
            "value": 0 // TODO: Fill in parameter value
        },
        "optionalObject": {
            "value": {
                "abc": "def"
            }
        }
    }
}` , longTemplate , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "requiredArray": {
            "value": 0 // TODO: Fill in parameter value
        },
        "optionalObject": {
            "value": {
                "abc": "def"
            }
        },
        "requiredString": {
            "value": "" // TODO: Fill in parameter value
        },
        "optionalInt": {
            "value": 1
        }
    }
}` , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "requiredArray": {
            "value": 0 // TODO: Fill in parameter value
        },
        "optionalObject": {
            "value": {
                "abc": "def"
            }
        },
        "requiredString": {
            "value": "" // TODO: Fill in parameter value
        }
    }
}` ) ; createAddMissingParamsTest ( "Don't put default value into params file if it's an expression" , stringify ( { "$schema" : "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#" , "contentVersion" : "1.0.0.0" , "parameters" : { } } , 4 ) , { "$schema" : "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#" , "contentVersion" : "1.0.0.0" , "parameters" : { "defValueIsBoolLiteral" : { "type" : "string" , defaultValue : true } , "defValueIsExpression" : { "type" : "string" , defaultValue : "[variables('bool')]" } } } , Params . all , `{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "defValueIsBoolLiteral": {
            "value": true
        },
        "defValueIsExpression": {
            "value": "" // TODO: Fill in parameter value
        }
    }
}` ) ; } ) ;