@platformatic/generators 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ 'use strict'
2
+
3
+ const { test, describe } = require('node:test')
4
+ const assert = require('node:assert')
5
+ const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, envObjectToString } = require('../lib/utils')
6
+ const { extractEnvVariablesFromText } = require('../lib/utils')
7
+
8
+ describe('utils', () => {
9
+ describe('stripVersion', async () => {
10
+ test('should return the same string if not semver', async (t) => {
11
+ assert.equal('no-version', stripVersion('no-version'))
12
+ })
13
+
14
+ test('should match semver', async (t) => {
15
+ assert.equal('1.2.3', stripVersion('v1.2.3'))
16
+ assert.equal('1.2.3', stripVersion('~1.2.3'))
17
+ assert.equal('1.2.3', stripVersion('^1.2.3'))
18
+ })
19
+ })
20
+
21
+ describe('convertServiceNameToPrefix', () => {
22
+ test('should convert service name to env prefix', async (t) => {
23
+ const expectations = {
24
+ 'my-service': 'MY_SERVICE',
25
+ a: 'A',
26
+ MY_SERVICE: 'MY_SERVICE',
27
+ asderas123: 'ASDERAS123'
28
+ }
29
+
30
+ Object.entries(expectations).forEach((exp) => {
31
+ const converted = convertServiceNameToPrefix(exp[0])
32
+ assert.equal(exp[1], converted)
33
+ })
34
+ })
35
+ })
36
+
37
+ describe('addPrefixToEnv', () => {
38
+ test('Should convert env and add prefix, if neede', async (t) => {
39
+ const testEnv = {
40
+ FOO: 'bar',
41
+ PLT_MY_SERVICE_NAME: 'service',
42
+ DATABASE_URL: 'foobar'
43
+ }
44
+
45
+ assert.deepEqual(addPrefixToEnv(testEnv, 'MY_SERVICE'), {
46
+ PLT_MY_SERVICE_FOO: 'bar',
47
+ PLT_MY_SERVICE_NAME: 'service',
48
+ PLT_MY_SERVICE_DATABASE_URL: 'foobar'
49
+ })
50
+ })
51
+ })
52
+
53
+ describe('envObjectToString', () => {
54
+ test('should convert env object to string', async () => {
55
+ const env = {
56
+ FOO: 'bar',
57
+ DATABASE_URL: 'sqlite://./db.sqlite'
58
+ }
59
+
60
+ assert.equal(envObjectToString(env), 'FOO=bar\nDATABASE_URL=sqlite://./db.sqlite')
61
+ })
62
+ })
63
+
64
+ describe('extractEnvVariablesFromText', () => {
65
+ test('should extract env vars from text', async () => {
66
+ const text = `
67
+ This is a sample text where an {ENV_VAR} should be detected
68
+
69
+ DATABASE_URL={DATABASE_URL}
70
+ `
71
+ const env = extractEnvVariablesFromText(text)
72
+ assert.deepEqual(env, ['ENV_VAR', 'DATABASE_URL'])
73
+ })
74
+
75
+ test('should not extract {} as empty env var', async () => {
76
+ const text = `This is a sample text where an {ENV_VAR} should be detected
77
+ but this {} should not be parsed
78
+ `
79
+ const env = extractEnvVariablesFromText(text)
80
+ assert.deepEqual(env, ['ENV_VAR'])
81
+ })
82
+
83
+ test('should return empty array if no env vars are detected', async () => {
84
+ const text = 'This is a sample text without any env var. This {} should not be parsed'
85
+ const env = extractEnvVariablesFromText(text)
86
+ assert.deepEqual(env, [])
87
+ })
88
+ })
89
+ })