|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright 2020 Electric Imp |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be |
| 15 | +// included in all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO |
| 20 | +// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
| 21 | +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + |
| 25 | +'use strict'; |
| 26 | + |
| 27 | +const Builder = require('../../src'); |
| 28 | +const backslashToSlash = require('../backslashToSlash'); |
| 29 | +const Log = require('log'); |
| 30 | +const path = require('path'); |
| 31 | +const fs = require('fs'); |
| 32 | + |
| 33 | +describe('Builder is called for file in included directory - ', () => { |
| 34 | + |
| 35 | + let builder; |
| 36 | + const contextPath = path.resolve(__dirname + "/../fixtures/include/sample-1/").replace(/\\/g, '/'); |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + builder = new Builder(); |
| 40 | + builder.machine.path = contextPath; |
| 41 | + builder.machine.readers.file.runDir = contextPath; |
| 42 | + builder.machine.readers.file.inputFileDir = path.join(contextPath + '/dirZ'); |
| 43 | + builder.logger = new Log(process.env.SPEC_LOGLEVEL || 'error'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should search Y file in directory where X file located', () => { |
| 47 | + let output = builder.machine.execute(`@include "dirZ/file_case1.nut"`); |
| 48 | + expect(output).toContain('// y.nut (case 1)\n'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should search Y file in directory where processing file located', () => { |
| 52 | + let output = builder.machine.execute(`@include "dirZ/file_case2.nut"`); |
| 53 | + expect(output).toContain('// y.nut (case 2)\n'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should search Y file in directory where builder called', () => { |
| 57 | + let output = builder.machine.execute(`@include "dirZ/file_case3.nut"`); |
| 58 | + expect(output).toContain('// y.nut (case 3)\n'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('file not found', () => { |
| 62 | + const filePath = path.join(contextPath, 'dirX/x_case4.nut').replace(/\\/g, '/'); |
| 63 | + const fileNotFoundMessage = `Local file "dirD/y4.nut" not found (${filePath}:1)`; |
| 64 | + try { |
| 65 | + builder.machine.execute(`@include "dirZ/file_case4.nut"`); |
| 66 | + fail(); |
| 67 | + } catch (e) { |
| 68 | + expect(backslashToSlash(e.message)).toEqual(fileNotFoundMessage); |
| 69 | + } |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('Builder is called for file in current directory - ', () => { |
| 74 | + |
| 75 | + let builder; |
| 76 | + const contextPath = path.resolve(__dirname + "/../fixtures/include/sample-1/dirZ").replace(/\\/g, '/'); |
| 77 | + |
| 78 | + beforeEach(() => { |
| 79 | + builder = new Builder(); |
| 80 | + builder.machine.path = contextPath; |
| 81 | + builder.machine.readers.file.runDir = contextPath; |
| 82 | + builder.machine.readers.file.inputFileDir = path.join(contextPath); |
| 83 | + builder.logger = new Log(process.env.SPEC_LOGLEVEL || 'error'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should search Y file in directory where X file located', () => { |
| 87 | + let output = builder.machine.execute(`@include "file_case1.nut"`); |
| 88 | + expect(output).toContain('// y.nut (case 1)\n'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should search Y file in directory where processing file located', () => { |
| 92 | + let output = builder.machine.execute(`@include "file_case2.nut"`); |
| 93 | + expect(output).toContain('// y.nut (case 2)\n'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should search Y file in directory where builder called', () => { |
| 97 | + const filePath = path.join(contextPath, '/../dirX/x_case3.nut').replace(/\\/g, '/'); |
| 98 | + const fileNotFoundMessage = `Local file "dirD/y3.nut" not found (${filePath}:1)`; |
| 99 | + try { |
| 100 | + builder.machine.execute(`@include "file_case3.nut"`); |
| 101 | + fail(); |
| 102 | + } catch (e) { |
| 103 | + expect(backslashToSlash(e.message)).toEqual(fileNotFoundMessage); |
| 104 | + } |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('Builder is called for file in deep included directory - ', () => { |
| 109 | + |
| 110 | + let builder; |
| 111 | + const contextPath = path.resolve(__dirname + "/../fixtures/include/").replace(/\\/g, '/'); |
| 112 | + |
| 113 | + beforeEach(() => { |
| 114 | + builder = new Builder(); |
| 115 | + builder.machine.path = contextPath; |
| 116 | + builder.machine.readers.file.runDir = contextPath; |
| 117 | + builder.machine.readers.file.inputFileDir = path.join(contextPath + '/sample-1/dirZ'); |
| 118 | + builder.logger = new Log(process.env.SPEC_LOGLEVEL || 'error'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should search Y file in directory where X file located', () => { |
| 122 | + let output = builder.machine.execute(`@include "sample-1/dirZ/file_case1.nut"`); |
| 123 | + expect(output).toContain('// y.nut (case 1)\n'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should search Y file in directory where processing file located', () => { |
| 127 | + let output = builder.machine.execute(`@include "sample-1/dirZ/file_case2.nut"`); |
| 128 | + expect(output).toContain('// y.nut (case 2)\n'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('file not found', () => { |
| 132 | + const filePath = path.join(contextPath, 'sample-1/dirX/x_case3.nut').replace(/\\/g, '/'); |
| 133 | + const fileNotFoundMessage = `Local file "dirD/y3.nut" not found (${filePath}:1)`; |
| 134 | + try { |
| 135 | + builder.machine.execute(`@include "sample-1/dirZ/file_case3.nut"`); |
| 136 | + fail(); |
| 137 | + } catch (e) { |
| 138 | + expect(backslashToSlash(e.message)).toEqual(fileNotFoundMessage); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + it('should search Y file in directory where builder called', () => { |
| 143 | + let output = builder.machine.execute(`@include "sample-1/dirZ/file_case4.nut"`); |
| 144 | + expect(output).toContain('// y.nut (case 4)\n'); |
| 145 | + }); |
| 146 | +}); |
0 commit comments