'use strict' ; import * as assert from 'power-assert' ; import * as fs from 'fs' ; import * as mock from 'mock-fs' ; import * as path from 'path' ; import { temporaryDir } from '../utils' ; import { load , normalizeTsConfig , resolveRulesDirectory , save } from '../tsConfigNormalizer' ; import { RawConfigFile } from 'tslint/lib/configuration' ; describe ( 'tsConfigNormalizer' , ( ) => { const altBase = 'alternative' ; afterEach ( ( ) => mock . restore ( ) ) ; describe ( 'normalizeTsConfig' , ( ) => { it ( 'should normalize tsconfig.json' , async ( ) => { const input = '/somewhere/tsconfig.json' ; const existentOnBase = 'existent-on-base' ; const existentOnAltBase = 'existent-on-alt-base' ; const existentOnBoth = 'existent-on-both' ; const nonexistent = 'nonexistent' ; const originalConfig = { rules : { someRule : [ Math . random ( ) ] , } , rulesDirectory : [ existentOnBase , existentOnAltBase , existentOnBoth , nonexistent ] , } ; mock ( { [ temporaryDir ] : { } , [ input ] : JSON . stringify ( originalConfig ) , [ existentOnBase ] : '' , [ path . join ( altBase , existentOnAltBase ) ] : '' , [ existentOnBoth ] : '' , [ path . join ( altBase , existentOnBoth ) ] : '' , } ) ; const expectedNormalizedConfig = { ... originalConfig , rulesDirectory : [ existentOnBase , path . join ( altBase , existentOnAltBase ) , existentOnBoth , nonexistent ] . map ( p => path . resolve ( p ) ) , } ; const actual = normalizeTsConfig ( input , altBase ) ; assert . equal ( actual . startsWith ( temporaryDir ) , true ) ; assert . equal ( actual . endsWith ( '.json' ) , true ) ; const actualNormalizedConfig = load ( actual ) ; assert . deepStrictEqual ( actualNormalizedConfig , expectedNormalizedConfig ) ; } ) ; } ) ; describe ( 'load' , ( ) => { it ( 'should load JSON' , async ( ) => { const fileName = 'json-with-comment.json' ; const json = `{
        // comment
        "key1": "value",
        "key2": 123,
        // key2: 456,
        /*
        key3: 789,
        */
        "key4": false,
        "key5": null
      }` ; mock ( { [ fileName ] : json } ) ; const expected = { key1 : 'value' , key2 : 123 , key4 : false , key5 : null } ; const actual = load ( fileName ) ; assert . deepStrictEqual ( actual , expected ) ; } ) ; it ( 'should load JS' , async ( ) => { const fileName = path . join ( __dirname , 'mockConfig.js' ) ; const expected = { key1 : 'value' , key2 : 123 , key4 : false , key5 : null } ; const actual = load ( fileName ) ; assert . deepStrictEqual ( actual , expected ) ; } ) ; } ) ; describe ( 'save' , ( ) => { it ( 'should save JSON' , async ( ) => { mock ( { [ temporaryDir ] : { } } ) ; const file = `/tmp/codeclimate-tslint/tsconfig-${ Math . random ( ) }.json` ; const expected = { saved : Math . random ( ) } ; save ( expected , file ) ; assert . deepStrictEqual ( JSON . parse ( fs . readFileSync ( file ) . toString ( 'UTF-8' ) ) , expected ) ; fs . unlinkSync ( file ) ; } ) ; } ) ; describe ( 'resolveRulesDirectory' , ( ) => { it ( 'should retain rules directory when it exists' , async ( ) => { const expected = 'single-dir' ; mock ( { [ expected ] : '' } ) ; const actual = resolveRulesDirectory ( expected , altBase ) ; assert . deepStrictEqual ( actual , path . resolve ( expected ) ) ; } ) ; it ( 'should resolve rules directory when it does not exist but does on alternative base' , async ( ) => { const dir = 'non-existent-dir' ; const expected = path . join ( altBase , dir ) ; mock ( { [ expected ] : '' } ) ; const actual = resolveRulesDirectory ( dir , altBase ) ; assert . deepStrictEqual ( actual , path . resolve ( expected ) ) ; } ) ; it ( 'should retain rules directory when it does not exist' , async ( ) => { const expected = 'single-dir' ; const actual = resolveRulesDirectory ( expected , altBase ) ; assert . deepStrictEqual ( actual , path . resolve ( expected ) ) ; } ) ; it ( 'should resolve string array' , async ( ) => { const existentOnBase = 'existent-on-base' ; const existentOnAltBase = 'existent-on-alt-base' ; const existentOnBoth = 'existent-on-both' ; const nonexistent = 'nonexistent' ; const expected = [ existentOnBase , path . join ( altBase , existentOnAltBase ) , existentOnBoth , nonexistent ] ; mock ( { [ existentOnBase ] : '' , [ path . join ( altBase , existentOnAltBase ) ] : '' , [ existentOnBoth ] : '' , [ path . join ( altBase , existentOnBoth ) ] : '' , } ) ; const actual = resolveRulesDirectory ( [ existentOnBase , existentOnAltBase , existentOnBoth , nonexistent ] , altBase ) ; assert . deepStrictEqual ( actual , expected . map ( p => path . resolve ( p ) ) ) ; } ) ; it ( 'should not resolve undefined rules directory' , async ( ) => { const expected = undefined ; const actual = resolveRulesDirectory ( expected , altBase ) ; assert . deepStrictEqual ( actual , expected ) ; } ) ; } ) ; } ) ;