|
| 1 | +import { Tree } from '@nx/devkit'; |
| 2 | +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; |
| 3 | +import { updateNxMavenPluginVersion } from './pom-xml-updater'; |
| 4 | + |
| 5 | +describe('pom-xml-updater', () => { |
| 6 | + let tree: Tree; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + tree = createTreeWithEmptyWorkspace({}); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('updateNxMavenPluginVersion', () => { |
| 13 | + it('should update the nx-maven-plugin version only', () => { |
| 14 | + // Arrange |
| 15 | + const pomContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 16 | +<project xmlns="http://maven.apache.org/POM/4.0.0"> |
| 17 | + <build> |
| 18 | + <plugins> |
| 19 | + <plugin> |
| 20 | + <groupId>dev.nx.maven</groupId> |
| 21 | + <artifactId>nx-maven-plugin</artifactId> |
| 22 | + <version>0.0.7</version> |
| 23 | + </plugin> |
| 24 | + </plugins> |
| 25 | + </build> |
| 26 | +</project>`; |
| 27 | + |
| 28 | + tree.write('pom.xml', pomContent); |
| 29 | + |
| 30 | + // Act |
| 31 | + updateNxMavenPluginVersion(tree, '0.0.8'); |
| 32 | + |
| 33 | + // Assert |
| 34 | + const updatedContent = tree.read('pom.xml', 'utf-8'); |
| 35 | + expect(updatedContent).toMatchSnapshot(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should not update other plugin versions', () => { |
| 39 | + // Arrange |
| 40 | + const pomContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 41 | +<project xmlns="http://maven.apache.org/POM/4.0.0"> |
| 42 | + <build> |
| 43 | + <plugins> |
| 44 | + <plugin> |
| 45 | + <groupId>dev.nx.maven</groupId> |
| 46 | + <artifactId>nx-maven-plugin</artifactId> |
| 47 | + <version>0.0.7</version> |
| 48 | + </plugin> |
| 49 | + <plugin> |
| 50 | + <groupId>org.apache.maven.plugins</groupId> |
| 51 | + <artifactId>maven-compiler-plugin</artifactId> |
| 52 | + <version>0.0.7</version> |
| 53 | + </plugin> |
| 54 | + </plugins> |
| 55 | + </build> |
| 56 | +</project>`; |
| 57 | + |
| 58 | + tree.write('pom.xml', pomContent); |
| 59 | + |
| 60 | + // Act |
| 61 | + updateNxMavenPluginVersion(tree, '0.0.8'); |
| 62 | + |
| 63 | + // Assert |
| 64 | + const updatedContent = tree.read('pom.xml', 'utf-8'); |
| 65 | + expect(updatedContent).toMatchSnapshot(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not modify pom.xml if file does not exist', () => { |
| 69 | + // Act & Assert - should not throw |
| 70 | + expect(() => { |
| 71 | + updateNxMavenPluginVersion(tree, '0.0.8'); |
| 72 | + }).not.toThrow(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should preserve XML formatting and structure', () => { |
| 76 | + // Arrange |
| 77 | + const pomContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 78 | +<project xmlns="http://maven.apache.org/POM/4.0.0" |
| 79 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 80 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 81 | + <build> |
| 82 | + <plugins> |
| 83 | + <plugin> |
| 84 | + <groupId>dev.nx.maven</groupId> |
| 85 | + <artifactId>nx-maven-plugin</artifactId> |
| 86 | + <version>0.0.7</version> |
| 87 | + </plugin> |
| 88 | + </plugins> |
| 89 | + </build> |
| 90 | +</project>`; |
| 91 | + |
| 92 | + tree.write('pom.xml', pomContent); |
| 93 | + |
| 94 | + // Act |
| 95 | + updateNxMavenPluginVersion(tree, '0.0.8'); |
| 96 | + |
| 97 | + // Assert |
| 98 | + const updatedContent = tree.read('pom.xml', 'utf-8'); |
| 99 | + expect(updatedContent).toMatchSnapshot(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should not update if version is already the target version', () => { |
| 103 | + // Arrange |
| 104 | + const pomContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 105 | +<project xmlns="http://maven.apache.org/POM/4.0.0"> |
| 106 | + <build> |
| 107 | + <plugins> |
| 108 | + <plugin> |
| 109 | + <groupId>dev.nx.maven</groupId> |
| 110 | + <artifactId>nx-maven-plugin</artifactId> |
| 111 | + <version>0.0.8</version> |
| 112 | + </plugin> |
| 113 | + </plugins> |
| 114 | + </build> |
| 115 | +</project>`; |
| 116 | + |
| 117 | + tree.write('pom.xml', pomContent); |
| 118 | + |
| 119 | + // Act |
| 120 | + updateNxMavenPluginVersion(tree, '0.0.8'); |
| 121 | + |
| 122 | + // Assert |
| 123 | + const updatedContent = tree.read('pom.xml', 'utf-8'); |
| 124 | + expect(updatedContent).toEqual(pomContent); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should handle pom.xml with dependencies and other version elements', () => { |
| 128 | + // Arrange |
| 129 | + const pomContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 130 | +<project xmlns="http://maven.apache.org/POM/4.0.0"> |
| 131 | + <version>1.0.0</version> |
| 132 | + <build> |
| 133 | + <plugins> |
| 134 | + <plugin> |
| 135 | + <groupId>dev.nx.maven</groupId> |
| 136 | + <artifactId>nx-maven-plugin</artifactId> |
| 137 | + <version>0.0.7</version> |
| 138 | + </plugin> |
| 139 | + </plugins> |
| 140 | + </build> |
| 141 | + <dependencies> |
| 142 | + <dependency> |
| 143 | + <groupId>junit</groupId> |
| 144 | + <artifactId>junit</artifactId> |
| 145 | + <version>4.13.2</version> |
| 146 | + </dependency> |
| 147 | + </dependencies> |
| 148 | +</project>`; |
| 149 | + |
| 150 | + tree.write('pom.xml', pomContent); |
| 151 | + |
| 152 | + // Act |
| 153 | + updateNxMavenPluginVersion(tree, '0.0.8'); |
| 154 | + |
| 155 | + // Assert |
| 156 | + const updatedContent = tree.read('pom.xml', 'utf-8'); |
| 157 | + expect(updatedContent).toMatchSnapshot(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should correctly identify nx-maven-plugin with whitespace', () => { |
| 161 | + // Arrange |
| 162 | + const pomContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 163 | +<project xmlns="http://maven.apache.org/POM/4.0.0"> |
| 164 | + <build> |
| 165 | + <plugins> |
| 166 | + <plugin> |
| 167 | + <groupId> |
| 168 | + dev.nx.maven |
| 169 | + </groupId> |
| 170 | + <artifactId> |
| 171 | + nx-maven-plugin |
| 172 | + </artifactId> |
| 173 | + <version> |
| 174 | + 0.0.7 |
| 175 | + </version> |
| 176 | + </plugin> |
| 177 | + </plugins> |
| 178 | + </build> |
| 179 | +</project>`; |
| 180 | + |
| 181 | + tree.write('pom.xml', pomContent); |
| 182 | + |
| 183 | + // Act |
| 184 | + updateNxMavenPluginVersion(tree, '0.0.8'); |
| 185 | + |
| 186 | + // Assert |
| 187 | + const updatedContent = tree.read('pom.xml', 'utf-8'); |
| 188 | + expect(updatedContent).toMatchSnapshot(); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should only update plugins, not parent version', () => { |
| 192 | + // Arrange |
| 193 | + const pomContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 194 | +<project xmlns="http://maven.apache.org/POM/4.0.0"> |
| 195 | + <parent> |
| 196 | + <groupId>dev.nx</groupId> |
| 197 | + <artifactId>nx-parent</artifactId> |
| 198 | + <version>0.0.7</version> |
| 199 | + </parent> |
| 200 | + <build> |
| 201 | + <plugins> |
| 202 | + <plugin> |
| 203 | + <groupId>dev.nx.maven</groupId> |
| 204 | + <artifactId>nx-maven-plugin</artifactId> |
| 205 | + <version>0.0.7</version> |
| 206 | + </plugin> |
| 207 | + </plugins> |
| 208 | + </build> |
| 209 | +</project>`; |
| 210 | + |
| 211 | + tree.write('pom.xml', pomContent); |
| 212 | + |
| 213 | + // Act |
| 214 | + updateNxMavenPluginVersion(tree, '0.0.8'); |
| 215 | + |
| 216 | + // Assert |
| 217 | + const updatedContent = tree.read('pom.xml', 'utf-8'); |
| 218 | + expect(updatedContent).toMatchSnapshot(); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments