ReplaceShadowPasswordEntry implements Statement { @Override public String render(OsFamily family) { checkNotNull(family, "family"); if (family == OsFamily.WINDOWS) throw new UnsupportedOperationException("windows not yet implemented"); try { String shadowPasswordEntry = cryptFunction.apply(password); String shadowFile = "/etc/shadow"; Statement replaceEntryInTempFile = exec(format( "awk -v user=^%1$s: -v password='%2$s' 'BEGIN { FS=OFS=\":\" } $0 ~ user { $2 = password } 1' %3$s >%3$s.%1$s", login, shadowPasswordEntry, shadowFile)); Statement replaceShadowFile = exec(format("test -f %2$s.%1$s && mv %2$s.%1$s %2$s", login, shadowFile)); return new StatementList(ImmutableList.of(replaceEntryInTempFile, replaceShadowFile)).render(family); } catch (Exception e) { propagate(e); return null; } } ReplaceShadowPasswordEntry(Function<String, String> cryptFunction, String login, String password); @Override Iterable<String> functionDependencies(OsFamily family); @Override String render(OsFamily family); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testAddUserWindowsNotSupported() { new ReplaceShadowPasswordEntry(crypt, "user", "password").render(OsFamily.WINDOWS); }